The code is working well because i can see my logs and every incoming notification on it but the challenge is how to get this on index page instead of having it on server-side logs. Thanks
You create a variable, for example, “notificationList”
Everytime you receive a notification, you add it to the variable
Then:
Your index.html sends a request every, let’s say, 0.6 seconds to an endpoint defined by the server. The server returns a JSON of every notification according to the variable notificationList.
Thanks so much, i now have the idea of how to get that done although i will be glad if simple sample that i can look up to achieve this. Just leaning but thank all the same for your time.
// Start your server.js with this:
var notifications = "";
// then add this when you recieve a notification:
notifications = notifications + "\n" + [NOTIFICATION_TEXT];
// Let's add an endpoint
app.post('/getNotifications', function(req, res){
res.status(200).json({ notifications: notifications })
});
Your index.html code
<script>
setInterval(function () { // declare an interval
fetch('/getNotifications') /* Fetch the endpoint */
.then(response => response.json()) // Make the response in JSON
.then(data => function () {
data = data.notifications;
// do what you want with "data" now
});
}, 600); // Fetch the endpoint every 600 milliseconds
</script>