views:

64

answers:

3

I have been meaning to implement a "who is online" feature on my site.

Was wondering how would you decide if a user is online or not?

Some options are:

  1. Last seen, less than N minutes ago (what is N?)
  2. A comet server with long polling
  3. Something else
A: 

If you are using session variables then the user is online if last_activity + session_expiry > current_date. Else the session has already expired and they are not online.

Now, it depends on what people will be able to do with this "who is online" feature. You might prefer a more conservative measure to have a higher confidence the user's active.

But, given the nature of the web, there's no sure fire way to ensure the user's really online and active in your site, short of requiring user interaction every once in a while, but that would be annoying.

Vinko Vrsalovic
would you go with 10 / 20 / 30 minutes?
Sam Saffron
@Sam: For the session expiry time?
Vinko Vrsalovic
how long since last activity is the user considered online?
Sam Saffron
last_activity + session_expiry_time
Vinko Vrsalovic
+1  A: 

I would go with option 1 and allow N to be set from a configuration file. Presumably user activity is being logged with a timestamp in some datastore, so calculating whether a user is considered online (seen less than N time ago) should be pretty straightforward. You may consider using a periodic AJAX request to update the online collection of users at regular intervals.

Russ Cam
+1  A: 

You could also use a ping method. Send a light ajax request from the client to server approximately every 30-60 seconds. Keep the request and response as small as possible to reduce bandwidth and this should perform almost as well as the Comet method.

Joel Potter
This method sounds good. First of all, when the user visits the site, ascertain if the user has AJAX, if they don't, use the classic timeout mechanism.
Chris Dennett
also I would have to determine if the tab is in the foreground ... i think google does this ..
Sam Saffron
How realtime do you want this feature to be? I'd say if they still have the site open than they are active. They don't necessarily need to be viewing the page. Of course it all depends on how you want to use the info.
Joel Potter
I just think it is poor practice to be constantly using bandwidth when a tab is not open ...
Sam Saffron