Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Server side even on browser window close using ASP.NET & C#

Posted on 2010-08-13
Medium Priority
Last Modified: 2012-05-10
Hi All,

I've just developed an chat application which works perfectly except for one thing. I would like to show the clients who's online and show's not, now showing online is easy. I just make IsOnline in the database true. And when the user clicks logout it's easy once again cause I do the same except false instead of true. But the problem comes in when the user closes the browser window I have no way of know if that user is offline, because I can't execute a server side event. I've tried overriding OnUnload in code behind, but the problem is when the user goes to a different page it executes that method which I don't want. So what I've figured is that asp calls the OnUnload event everytime the client leaves that page.

Any help would be appreciated,
Question by:SeanNij
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 53

Expert Comment

by:Ryan Chong
ID: 33427658
You can design a script where every X seconds of interval, it sends a request to your web application (this can be done by using AJAX, etc), and that tells the end user is still "alive".

In your request sent to your web application, try stored the "last request date" field in your table, so that we will not when was the last time the end user "ping" to your web application.

So, if the "last request date" is exceeded Y seconds, we categorized it as "offline".

Hope this approach make sense.
LVL 18

Expert Comment

by:Ajay Sharma
ID: 33427700
You can use

to check that if the client is still connected to the server and not closed his browser.

Author Comment

ID: 33427787
Thanks Ryancys

Was a great idea, but the problem I have with this is every time the timer ticks the page stands still which means that while the timer does it's business the whole page stops and I don't want that, because the user is busy chatting there. If he's busy typing "Hello How Are you?" while the timer is bust loading then it stops the user from typing in the middle of the sentance and will come out something like "Hel re you?". Very annoying, so can't use that solution, but thanks anyway.


I don't understand how that would work. If the user closes the browser my web application disappears, so how can I execute a method on a web page when it's not even open anymore? Maybe I'm understanding wrong, but that's how I see it...

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 33428131
alternatively, you can use javascript to do the job

With java script just check if browser is closing if yes then send a signal to server about the user status.
you can look at this http://codingforums.com/showthread.php?t=37279 

Hope this helps :)
LVL 28

Expert Comment

ID: 33428504
You need to define what IsOnline means.

If someone surfs to another page, is this person still online?
If some disconnects the cable by accident, and reconnects it after 5 minutes. Has this person been offline during that time? And how do you know the difference between a toilet visit and unplugging the cable?

> I just make IsOnline in the database true

It is easier to have IIS handle it. Use the Session object and more specifically the SessionOnStart() and SessionOnEnd() methods. As long as someone has a session, consider this person "online". If the person does not logout, the Session will time out eventually (usually 20 minutes).

You can also keep track of the time of the last activity of each session. So you can display a list of online users, and for each user list how many minutes ago the last activity was. Then everyone seeing that can decide for themselves if some who has been inactive for 10 minutes is still online or not.

Author Comment

ID: 33428601
Hi Sybe

IsOnline would be stating that the user is on the website no matter where... Redirecting to another page is still online (think of facebook)

Ok that's a great idea, but can I maybe ask if the user closes the browser and the session expires after lets say 20min, will the Session_Expire(object sender, EventArgs e) method in global.asax still fire even though the website has been closed down?

LVL 28

Accepted Solution

sybe earned 2000 total points
ID: 33428689
> the user is on the website no matter where

That is not good enough.

> the session expires after lets say 20min, will the Session_Expire(object sender, EventArgs e) method in global.asax

Yes, because it runs on the server, not on the client. IIS is the keeper of the sessions, and IIS will expire sessions after 20 (or xx) minutes of inactivity. All the user needs to do is be inactive (away, shut down PC, burnt down the house or whatever reason there is to no longer request any new pages from your application).


Author Comment

ID: 33428762

Thanks for all the help.


Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question