2

I'm doing a real-time website with Node.js (Express framework) and socket.io, at some point it has to detect if someone is 'online' (which is basically if someone has the webpage opened).

I think Facebook chat is a group example for this. But what's the engineering logic behind it?

Thanks a lot!

Maria
  • 3,455
  • 7
  • 34
  • 47

3 Answers3

1

This slideshow(http://www.erlang-factory.com/upload/presentations/31/EugeneLetuchy-ErlangatFacebook.pdf) from Facebook explains its Messaging System in detail. The system has comet(channel) clusters, which keep TCP connections with browsers, and periodly push connected user list to a Presence Server. So the web page(PHP) can query Presence Server if a user is online or not.

ideawu
  • 2,287
  • 1
  • 23
  • 28
  • Facebook changed this a long time ago. They use an XMPP server now which detects presence and broadcasts rather than by long polling like you described. – Drizzle Apr 19 '23 at 05:16
0

Not sure exactly how Facebook does this but if you serve a page to the user that has javascript in it, which makes asynchronous requests at a specific interval say every minute. Then when you receive a request the user is marked online with a timeout slightly greater then the ajax interval. If the time-out expires without another ajax request the user is marked offline.

You could possibly hook into the page load and page close events to only send requests when a user loads the page or leaves it as well. This would require less requests.

EDIT

I see your using socket io I'm not super familiar with this but I imagine you could use the socket connection / disconnection events provided.

djbrick
  • 213
  • 1
  • 4
  • Thanks for the answer. I'm currently using the page load/close event, but it get messy when the user has the webpage opened in more than one tab. It can also mess up when the user keep press F5 like crazy – Maria Aug 29 '13 at 03:48
  • yeh, I guess the page close event would cause issues with multiple tabs. http://stackoverflow.com/questions/12166187/manage-multiple-tabs-but-same-user-in-socket-io this might help – djbrick Aug 29 '13 at 03:53
  • http://stackoverflow.com/questions/1051895/whats-the-easiest-way-to-determine-is-a-user-in-online-php-mysql the pinging on an interval seems to be a common solution – djbrick Aug 29 '13 at 04:00
0

The client sends keep-alive messages to the server periodically, implemented in client-side JS.

If the server doesn't receive the keep-alive in some time-out period, it can mark the user offline. It can also do so when the websocket is explicitly closed.

WeaponsGrade
  • 878
  • 5
  • 13