HACKER Q&A
📣 thescribbblr

What is the best way to check if a user is online on your website?


Hi HN,

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.

But still these two methods are not working well.

Can somebody assist me the best way to check the online activity and mark a user online. I am looking for an easy way.

Please help.


  👤 muzani Accepted Answer ✓
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.


👤 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.


👤 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.