views:

140

answers:

6

First let me say that I really feel directionless on this question. I am using windows integrated security, and I can use vb.net to look up information about a user from AD. I also have other information about users I can look up from a MS SQL 2005 server by getting the logon identity name.

What I would like to do is display information about all the users actively viewing the web page to any one of the users viewing the web page. The information comes both from AD and SQL, and I have no problem retrieving it.

My route so far has been using SQL to store when the user first loads the page. I am stuck not knowing how to show when the user leaves the page. I tried using an ajax timer to update a timestamp for the user's visit every one second that also triggers the table to change the status to inactive of any record that has not been updated in 5 seconds. This works with only a few users, but I find when I have more than a few people viewing the page the 1 second update is not reliable. I also seem to have problems when the user minimizes the page. This sometimes stops the updates from the ajax timer and kicks the user off the list while they are still viewing the page.

This feature is not important to the function of the site it would be on, so I'd given up on it over a year ago. Since then it has really been a pain to me that I can not figure a way to make this work. My searches have led me down many fruitless paths, so I really will appreciate any help that can be offered even if it's only a lead in the correct direction.

+1  A: 

I think the best you can do is set a threshold for "visiting a page." Have an automated task run every 60, 120, 300, or some number of seconds that clears out any entry that is older than a specified amount of time. There is no way to reliably detect (that I am aware of) when a user leaves a page. The best you can do is "assume" a user has stopped using the site if a certain amount of time has elapsed. So you would store the user, the page, and the time viewed. Once that time viewed has surpassed your threshold, remove it.

lordscarlet
I think that would work, but it would also compromise the functionality of the page. Users could potentially use the page for up to two hours. It would also mean a delay when they leave an it is updated. It's really important to functionality that aspect is minimized.
K Richard
There is no other 100% reliable way to do it. You can try to do AJAX to keep hitting the database, but that is unreliable and seems like unnecessary traffic to the database.
lordscarlet
A: 

For the body have an onunload script: < body onunload="userLeftPage()"

In that script, send an ajax call to say the user left the page.

Matt
I have looked at this once - my memory is a little foggy - but I think I remember that that onunload is triggered at times when I would not want it, like at postback.
K Richard
If the user walks away from their computer for the next 16 hours, or the power fails, onunload will never be called.
Grant Wagner
+1  A: 

I don't think having an AJAX request every second is a good idea, it's way too chatty.

I think most people implement this feature by just recording when someone makes a request to the site and from that time to threshold the user is 'visiting' the site. If the user doesn't make another server request before the threshold is reached then we assume that they have moved on.

SCdF
Well, it's really an issue of the functionality of the site. The user's information their would indicate they are available to answer questions for agents and to view stats as they update. There could be lengthy periods of inactivity.
K Richard
That's a slightly trickier thing then. If you require the list of current users to be accurate for some site functionality as opposed to kinda-accurate for interests sake ('ohh look, John's using the site too!') then that's a different situation.
SCdF
Some other things to think about: When you say they are available to answer questions, what if I leave the site open then go for coffee? Or go home for the night? This sort of thing is not really possible from a webpage if you wish for lengthy periods of inactivity to still count as online correctly
SCdF
If you want users to be that connected then honestly a better solution would be some kind of thick-client IM; jabber, skpye, msn. Thick client apps can do better guesses at availability via checking for activity of the mouse / keyboard etc, which your web app can not do.
SCdF
A: 

How about having a tiny flash app on the page that streams a minuscule 'heartbeat' stream of data from the server....just enough to allow the server to know when a stream had been dropped, and hence when the client had navigated away from the page.

Dan
+2  A: 

The answer probably depends on how accurate you need the display to be. If it's just to give users a sense of the other people using the site I'd suggest something similar to what you've described, but backing off on the update frequency:

  • on a page request associate the user with the page (and a timestamp)
  • use an Ajax timer to update the timestamp every minute or so
  • kill the association via a window.onbeforeunload event (or similar)
  • assume that any timestamps older than a minute (and a bit) are dead

You can try and catch some of the ways people leave a page, but it's never bullet proof. And with regards to the minimised page, I guess it's debatable whether they're actually viewing the page ;)

David Bick
A: 

The first answer works.

BUT...

You didn't mention that the users were logged in, so I suspect that isn't what you are looking at. Though, you can certainly simplify things if you are just requesting the list of logged in users every minute or so.

Matt