Discussions

Web tier: servlets, JSP, Web frameworks: What is the best way to control online users?

  1. I gotta keep a list of online users, so when I list all of them I put some green flashling light (or whatever) on the "online" ones.

    By online, I mean active, cuz there's no perfect way to tell if a user has its browser open on the website right? So what would be the best way to determine which are the online users?

    Thanks,
    Christian
  2. Hi,

    You can implement a HttpSessionListener for your web application. When user logs on to the system, create a session and a keep the userid in sesssion scope. In sessionCreated() of HttpSessionListener imlementation class, add the user ID to the ArrayList and maintian it in application scope. Whenever you reach the page to show the online users, cross check all the users with arraylist stored in application scope to show the online users. When user logs out or session time out heppens, the sessionDestroyed() of HttpSessionListener imlementation class will be called and there remove the user ID from the arraylist which is in applicaion scope.

    I think this solution works pretty fine.


    Anil Sadineni.
  3. session timeout?[ Go to top ]

    I don't think session timeout is a good way to determine if a user is online, cuz that's like 30min. It's too long, maybe a user has been on for as little as 5min, and 25min afterwards it'll still be showing as if he was actually on.

    I can't set session timeout to a value like 5min or so or it would keep timing out on users who are actually online an navigating (maybe he stopped to read something, or whatever) on the site.

    But comming to think of it, I don't see any other way. :(
  4. session timeout?[ Go to top ]

    Perhaps you could implement your own isOnline() criteria... No matter the session timeout, if the user has not performed any action for some time, show it as "offline" (or "away").
    This could be accomplished using a combination of the HttpSessionListener approach described earlier in the thread, an a Filter intercepting user actions (an "action" being every request). You should store the time of the last action in the Session (kind of a parallel timeout, rite?).
    Of course, even this crashes if you have any other applications action as client-side, besides your web-app. In this case, you should push this behaviour further down the application layers.

    Cheers and happy coding,
    Martin
  5. Using a tiny flash movie (or applet) in a frame, you could open an XMLSocket to a server. As soon as the browser window gets closed, it will disconnect. That's probably the most correct way to do it, but it a) might give permission troubles b) requires a custom server, and c) requires the use of Flash, of course.

    Then again, I'd go for the session creation/delete approach, much cleaner, less to worry about.
  6. Hmmm... clever... Kind of dirty, but clever, I'll give you dat. A nice, little, invisible agent in the client. Still, there's a lot to consider with this approach, I think. Didn't thought about it long enough, but I sense that openning two browsers windows your give you some hard time... I mean, 2 applets, 2 sockets, blah blah....
    Have you actually implemented this?

    Cheers and happy coding,
    Martin