3 ms·
Ask HN: 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.
- tux 7y agoWhy 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 7y agoActually I am looking to count logged in users say for example in a chat room.
- muzani 7y agoWe 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 7y agoThank you so much. Can you please the elaborate the entire procedure so that I can achieve the same in my application.
- muzani 7y agoThe 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 7y agoAwesome! Thank you so much.
- photawe 7y agoIf 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 7y agoThis way looks more impressive and clean. Thank you so much.