2

I'm using socket-io on top of node to manage a chat app.

I was wondering, once a message is recieved, how can I check for a "seen" and "unseen" status of the message. Is there an event on jquery? I'm not sure if i should manage this from the jquery or socket-io side. Thank you!

1

4 Answers 4

7

You have to implement your own logic as to send response back when message is read. There is no such socket-io jquery event to trigger on read .Yon can emit a message on message received by checking that if the tab is currently active as that is the way you can consider a message to be read if the tab is currently active or your chat window is opened.

2
  • Thanks! If i may ask as to complete the question, how would I go on checking if a tab is active or not? Commented Mar 15, 2016 at 19:51
  • @J.Gal - you would try to set a flag on the window.onfocus and window.onblur events. The following snippet has been tested on Firefox, Safari and Chrome- var isActive; $(window).focus(function() { isActive = true; }); $(window).blur(function() { isActive = false; }); Commented Mar 16, 2016 at 7:12
4

You will have to send a message back when the message is read. There is no notion of when something is "read" in socket.io.

That's something you would have to invent in your own user interface and when you consider it read, you could send a message back to the sender that indicates it is now read. Unless each message requires some action to open it in order to read it (like an email often does), it's likely hard to know when it was actually "read" by the recipient. In typical chat programs, the message is just displayed automatically and you have no idea whether the recipient is looking at it or not.

5
  • That makes sense, however that "read" would mean the message was "delivered"', not actually read by the user. Example: Imagine the user is browsing reddit on one tab, and the message is delivered. It's still not read. That's what i want to differenciate. Commented Mar 12, 2016 at 16:37
  • @J.Gal - Wouldn't your server know when it was delivered? Are you queuing messages in your server in case the recipient is not online and then when the user comes online you send it? If that's the case, then you just need to send a message back to the sender from your server when your server can deliver it to the recipient. You don't explain anything about your server architecture here so this is just an idea.
    – jfriend00
    Commented Mar 12, 2016 at 16:39
  • Yes, i understand what you mean, but again, my interest is in knowing when the message was read, not delivered. I don't think the server has anythinig to do there, it's up to the client to determine if the user is in the chat page or not, i imagine. Commented Mar 12, 2016 at 16:43
  • @J.Gal - If the browser does not have the chat page loaded, then there is nothing to send the chat message to. Your server won't be able to deliver it. Your server should be able to tell the sender whether that is the case or not. If the chat page is loaded in the recipient browser, then the code in that page can decide what you want "read" to mean and can send a "read" message back to the sender whenever it thinks that condition has been met.
    – jfriend00
    Commented Mar 12, 2016 at 16:46
  • @J.Gal - You aren't describing very much detail about how you want this to work or how your system works so it's hard for us to make concrete recommendations. Your question could be improved a lot by editing it to explain all this extra detail of your architecture and what you want "read" to mean.
    – jfriend00
    Commented Mar 12, 2016 at 16:47
1

For managing seen/unseen we need to fire background message after receiving/reading from user , which is like acknowledgement receipt. It is simple.

0

There are two things to do. First, check if the browser is currently active. And second, check if an element is visible in the viewport. When an element is in the viewport, it appears in the visible part of the screen. From then on, you can emit a message to socket with read

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.