I have been wondering how to identify a user when he is online. What is the mechanism to use it.. I know I have to use HttpSession object for this. But i don't have the slightest idea about this..
My exact requirement is
"When a user is online, The status message in his profile will display online. The user is permissible to view other user profiles (in non-edit mode). When the user views other profiles, display must show whether other profile users are online or offline."
The only mechanism you have to know whether someone is accessing your web application is a request. If the user makes a request they are "online". HTTP is stateless, so once that request is finished you can't really tell whether the user is still online, until they make another request. So you can probably pick an arbitrary number (say 5 minutes) and record who made a request in the last fime minutes. You could use the session for this; you may want a shorter check time than a usable session timeout would allow though. You also may want to add mechanisms to get a quicker response if they navigate away or close the browser, some sort of request fired on page unload should do that.
I think what Paul says, is happening in Javaranch forum. Because within a shorter time a user is asked to reauthenticate...
My guess is so..
Bear Bibeault wrote:I think that the answer greatly depends upon what you define as "online".
When a user is authenticated, He is free to navigate his profile as well as other profiles. He is free to modify his profile and only allowed to view other profiles. The time between the user authentication and user logoff is what i mentioned "online". User Logoff can also be automated through session timeout.
My requirement is User page must display the message "online" or "offline". Help me out.
Paul's post contained a possible approach; if that's not workable in your case, tell us why.
Ping & DNS - updated with new look and Ping home screen widget
Joined: Aug 12, 2009
Guys, I got the idea of Paul. But I am asking you some of the ways to implement.
If i am going to use Paul guidelines, then each and every request is going to be checked before it serves its original purpose. That's what i was thinking about. For example,
An authenticated user is displayed online because he is constantly making requests. But when suddenly he clicks a link to some other profile. This request has to bring all the details about the other person's details like his name, profession, location etc. All the details can be fetched from database. Now I must also store the info about the person is online or offline. So that i can fetch the message whether he is online or offline to the viewing user.
I am asking the way to implement these. I think i am making my point here.
Joined: Mar 22, 2005
You don't need to store the information whether someone is online in a DB. I'd use an in-memory Map<userID, lastAccessTimestamp>. You can have a background job that removes entries from this list if the timestamp is older than your definition of "online"; you could have that running every minute or so.
You also need to update that list every time a user accesses the site, so you need to put in place proper synchronization.
Joined: Aug 12, 2009
Thanks buddy, i was sprinting around answers like this. Before i am cleared up.. How can i run the every-minute-or-so job in web app ( I mean standard way of doing this). I am wondering how can i run a code that's routinely checking for a particular interval. It must sound like Servlet Listeners....