How to determine if any users are currently viewing website?
-
Hello. This may or may not be an asp.net question but basically, we have a website & we have a situation where we need to know instantly if any users are viewing the site. I know I can implement some code to create a session when a user visits the site and then log into the database when that user's session has expired (i.e. he's off the site), but even that wouldn't be completely accurate since session objects wait for about 20 minutes of inactivity before expiring. What I need is be able to determine whether any users are on the site currently, not within the last 20 minutes. My other thought was perhaps there's some tools on the server itself that can help me with this (we use Windows Server 2003), but I wasn't sure if this was a possibility. Any ideas on the best way to track this? Thanks.
-
Hello. This may or may not be an asp.net question but basically, we have a website & we have a situation where we need to know instantly if any users are viewing the site. I know I can implement some code to create a session when a user visits the site and then log into the database when that user's session has expired (i.e. he's off the site), but even that wouldn't be completely accurate since session objects wait for about 20 minutes of inactivity before expiring. What I need is be able to determine whether any users are on the site currently, not within the last 20 minutes. My other thought was perhaps there's some tools on the server itself that can help me with this (we use Windows Server 2003), but I wasn't sure if this was a possibility. Any ideas on the best way to track this? Thanks.
you can track pages loading and unloading via javascript. that way whenever a page loads or unloads you can call some server side code via ajax.
<body onload="alert('The page was loaded')" onunload="alert('The onunload event was triggered')">
</body></html>
as if the facebook, twitter and message boards weren't enough - blogged
-
you can track pages loading and unloading via javascript. that way whenever a page loads or unloads you can call some server side code via ajax.
<body onload="alert('The page was loaded')" onunload="alert('The onunload event was triggered')">
</body></html>
as if the facebook, twitter and message boards weren't enough - blogged
This will cause the event to be called every time the page is loaded, i.e. from a refresh or reload. That would not be useful for the OP.
No comment
-
Hello. This may or may not be an asp.net question but basically, we have a website & we have a situation where we need to know instantly if any users are viewing the site. I know I can implement some code to create a session when a user visits the site and then log into the database when that user's session has expired (i.e. he's off the site), but even that wouldn't be completely accurate since session objects wait for about 20 minutes of inactivity before expiring. What I need is be able to determine whether any users are on the site currently, not within the last 20 minutes. My other thought was perhaps there's some tools on the server itself that can help me with this (we use Windows Server 2003), but I wasn't sure if this was a possibility. Any ideas on the best way to track this? Thanks.
You could put code in the SessionStart event to record when the user comes to the site. Or in the Login process if you use that. Capturing when the use leaves the site is more difficult. There have been many discussions here about how to get information when a user leaves a site. You may want to try a little searching here. It does involve JavaScript to call a server method.
No comment
-
This will cause the event to be called every time the page is loaded, i.e. from a refresh or reload. That would not be useful for the OP.
No comment
for knowing when things start the best approach then would be to possibly tie it into the
Application_Start
method in the Global.asax. However knowing when they have left your sight will have to be tied into the page unLoad event. the problem with that approach is that leaving a page doesn't mean leaving the application, as you could just be leaving for another page in the application. this was why I tied into the page onLoad event but you are right there are other approaches. much like skinning a cat there is always more than one to do it. :-D Another approach maybe.... Tie into theApplication_Start
and then have an interval on your pages so that every so many seconds (configurable) a web method is called updating the fact that the user is still on your web page. Many moons ago in this very galazy I worked on a web based chat application that did something very similar.as if the facebook, twitter and message boards weren't enough - blogged
-
for knowing when things start the best approach then would be to possibly tie it into the
Application_Start
method in the Global.asax. However knowing when they have left your sight will have to be tied into the page unLoad event. the problem with that approach is that leaving a page doesn't mean leaving the application, as you could just be leaving for another page in the application. this was why I tied into the page onLoad event but you are right there are other approaches. much like skinning a cat there is always more than one to do it. :-D Another approach maybe.... Tie into theApplication_Start
and then have an interval on your pages so that every so many seconds (configurable) a web method is called updating the fact that the user is still on your web page. Many moons ago in this very galazy I worked on a web based chat application that did something very similar.as if the facebook, twitter and message boards weren't enough - blogged
The Application_Start event would not be useful either. This event is fired when the application is started or the application pool is recycled. It has nothing to with individual sessions. The Page.Unload event is fired when the ASP.NET engine has completed processing, before the page has been rendered to the client and happens each time the page is processed, such as during a postback. I believe you should review the ASP.NET application and page life-cycles.
No comment
-
The Application_Start event would not be useful either. This event is fired when the application is started or the application pool is recycled. It has nothing to with individual sessions. The Page.Unload event is fired when the ASP.NET engine has completed processing, before the page has been rendered to the client and happens each time the page is processed, such as during a postback. I believe you should review the ASP.NET application and page life-cycles.
No comment
Mark Nischalke wrote:
Page.Unload event
my mistake I was not clear, I was referring to the unload for javascript against the
body
of the page. When I read this last night I was scratching my head thinking what are you talking about Page.Unload?? :confused: when I woke up this morning and read it all again I was like Homer Simpson... :doh: I believe from the Global functions you should be able to find when the session starts. As far as finding when the user leaves the site... Well I think there are many approaches to find this out, each of them with their own download. If you are trying to get something as close as possible to the real event (the user is no longer at your site) then I imagine you are going to have to tie it all into some interval based javascript code that is calling a server function. For that matter though I wonder if you could also just lower your session timers on the web server and once again use one of the Global functions. I am sure this would make a great article (one probably already exists) to write sometime. :)as if the facebook, twitter and message boards weren't enough - blogged