
Ask HN: What is the best way to check if a user is online on your website? - thescribbblr
Hi HN,<p>I am trying to check the online activity of a logged in user on my website. For now, I am checking (in every 5 minutes) if a user is online if she scrolls upto certain part of the webpage. Or, if she switch from page A to page B or vice versa.<p>But still these two methods are not working well.<p>Can somebody assist me the best way to check the online activity and mark a user online. I am looking for an easy way.<p>Please help.
======
muzani
We used a "heartbeat". The client sends a heartbeat every N minutes (default
5, set by server), and the server responds with an acknowledgement.

It doesn't interfere with performance much (we scaled it to millions of
users). Though the other trick was to use it as a way to bundle cache
information.

We also sent it to notify the server whenever it quit the tool. But there are
cases where it doesn't (device failure), so the server assumes the person is
off when it misses 3 heartbeats.

~~~
thescribbblr
Thank you so much. Can you please the elaborate the entire procedure so that I
can achieve the same in my application.

~~~
muzani
The easiest way would be to set a timer on the client side every N minutes. If
the server hasn't received a heartbeat from the client in 3N or 2N minutes,
assume that they're disconnected.

Also set a disconnect request whenever the app/site closes.

~~~
thescribbblr
Awesome! Thank you so much.

------
tux
Why not simply monitor your web-server log? (Example: tail -f
/var/logs/nginx.log) you can even code a custom bash script to filter specific
pages from your server log and show that instead. Then you can see what pages
users view in real-time, what they search etc...

But if you don't want to code anything, try using sites like Google Analytics.
Simply embed small javascript code and your done. After that login to your
account and use Google dashboard.

~~~
thescribbblr
Actually I am looking to count logged in users say for example in a chat room.

------
photawe
If the only thing you want to be able to mark users online - I would do this
-> whenever the user does something, update its "last_seen_time" to
DateTime.Now

If a user doesn't do anything for lets say, 5 minutes, you can mark it as
offline.

~~~
thescribbblr
This way looks more impressive and clean. Thank you so much.

