I’m building a one on one chat application that allows users to host rooms. Just to give a little more feedback:
-
User creates room (rooms are public, anybody can view but if user hasn’t signed up, chat will be disabled and user will be prompted to sign up for chat.)
-
Other users sign up for room
-
Users chat with host based on order they signed up
I would like to show if the host is online or not. Or to go one step further, show if the host is in the room.
I’m not sure how to do this.
Basically, this things work as following.
Every time user take actions (chat refresh, message sent, page visited, etc) a token associated to the user (token that is saved in your server, DB for example) is updated with current time.
And then you decide an user is online if his token is not too old (5/10 minutes?)
You can improve this method if you use AJAX to send a periodical request to server every 1 minute or so. This AJAX request update the token value. So, this way, you can tell the user is online also if he does nothing but stare at the page. And when he quit (close browser or tab), AJAX periodical call stops works, so after 5/10 minutes the user is declared offline.
Good luck!