How to update a web page in reaction to a push-notification?


How to update a web page in reaction to a push-notification?



I am exploring the world of push-notifications in Node.js and have one question.



Following a few tutorials, I now have a small app firing a few notifications and those pop up in my web browser as expected.



Here is the question:



Instead of having a notification popping up, would it be possible to have a field in my web page updated?
(For example: a clock or a counter)



If the answer is YES, how can I do it? Do I need to change the service worker? The client? Or what?



Here is the current code:


const SECS = 5 * 1000;
let rankVal = 0, counterVal = 1;
const notificationLoop = setInterval(() => {
let currentDate = new Date(),
timeLag = (currentDate-referDate)/1000
const payload = JSON.stringify({
title:'Event is reached!',
timeLag:timeLag.toString()
});

webPush.sendNotification(subscription,payload).catch(err => console.error(err));
console.log('-- setInterval --' + counterVal + ' : ' + rankVal);

if (counterVal>262144) {clearInterval(notificationLoop);}

rankVal += 1
counterVal *= 2

}, SECS);




1 Answer
1



Check out pusher, should do exactly what you want:
https://pusher.com



Note, you can use pusher for popups and stuff aswell, but mainly it is really good for keeping track of fields and changing data if someone else is using the site at the same time, etc.





Thanks for the suggestion, I had a quick look at this pusher.com; I created an account and it seems there is a lot to learn there. But I would be happy if I could get what I need, without having to dig into a whole new world.
– Michel
Jun 29 at 9:41





Well, event listeners is what you want to look for then. But it is a new world entirely just compared to plain javascript. But you could always as an easier and a bit more lazy method (Not as good, by far) create an javascript that checks every now and then if the data has changed, if changed - replace - if not, wait until next refresh. If that is an option, let me know. I do have some example code.
– Stoff
Jun 29 at 9:58





Well, I guess that is better for me at this point, because I already have something working with a client and a service worker. It is just that, since my experience is so limited that I don't really know yet how to use what I have.
– Michel
Jun 29 at 10:10





setInterval(function() { $(document).ready(categoriesSpinner); $(document).ready(categories); $(document).ready(specificFaultsSpinner); $(document).ready(specificFaults); $(document).ready(timeOfRefresh); setTimeout(function() { console.log('Refreshing numbers every minute.') }, 15000); }, 60000); replace the functions with whatever api function you may have created and you're golden then.
– Stoff
Jun 29 at 11:04





Should I keep the "$(document).ready(.....);" parts, the way they appear or am supposed to replace them by something else?
– Michel
Jun 29 at 13:00






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV