Reputation: 1549
I am doing notifications about new messages. I think I need open socket for each user and server will send notification when a new message. I want to use socket.io and redis pub/sub. When user send messages, it's publishing this event to redis and nodejs server that will be subscriber and will notify client about new message. But in this case, the message immediately will be removed from the queue and when the user updates the page he receives notify that all messages are read because the queue is empty, but actually messages aren't read. How to decide this problem?
Upvotes: 2
Views: 3089
Reputation: 4222
I already did something like this for a multi-page web app, but now it's single-page and I don't need this anymore:
var notifications=[]
...
socket.on('notify', function(message)
{
console.log('new notification received!');
if(notifications[message.notificationId]!==message.notificationId){
//notification never received in the current page
notifications[message.notificationId]=message.notificationId;
//display the notification code here
//In a closure instead of 3 params in order to keep compatibility with IE
setTimeout(function(){Confirm(message.notificationId);}, CONFIRM_INTERVAL);
}
else{
//notification already received
emitConfirm(message.notificationId);
//or setTimeout(function(){Confirm(message.notificationId);}, CONFIRM_INTERVAL);
}
});
Upvotes: 1
Reputation: 4581
If I understand correctly, all users keep a web socket open with socket.io, any new messages are immediately sent to all users and marked as unread. The problem is simply that if the user refreshes then the app does not know what messages to mark as read vs. unread.
In that case, your messages could have a timestamp and you could record the timestamp of the last message that each user actually read (e.g. emit a message-read event from client to server), so that when a user refreshes the page there is still information from which to reconstruct the list of read and unread messages.
Upvotes: 0