Online Users

Hi all!

Im trying to create some kind of forum, and I want to know how many users are online in each of the topics (not in the main website). I can create a table in te database that counts the users that enters each Topic actionView($id), but the problem is that if suddently one closes the chrome… I do not know how to decrease the online users of that topic.

Is there any way to do it with sessions (in my website the ussers have to sing in and sign out).

It will be very easy if there is a method that triggers once a users close the browser suddenlty without clicking sign out action.

Thanks for your help!

If precision for number of online is very important then you might need to have setInterval of javascript and send ajax to update it.

Or you can rely on last_seen (datetime when user saw that form) in this way you can simply consider user is viewing topic if this is in between 0 to lets say 5 mins?

You can’t really rely on window close event as it might not sent request before you can actually close window. If there is a delay, users will wonder why close didn’t work :)

First of all, you should decide whether user is considered online if he just forgot to close the page.

For example, one can open your URL in background and then go to cafe. Is he online?

And only then you can write your code. See Varun Batra’s comment for details.

PS. I prefer using ajax interval updater (‘heartbeat’).

More important though, most browsers don’t support it.

AFAIR, onBeforeUnload/onunload works in most browsers (except Opera). But yes, it’s not reliable at all.

Ok!! All the information was very useful!!