Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 813
  • Last Modified:

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

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,
1 Solution
Ryan ChongCommented:
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.
Ajay SharmaCommented:
You can use

to check that if the client is still connected to the server and not closed his browser.
SeanNijAuthor Commented:
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...

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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 :)
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.
SeanNijAuthor Commented:
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?

> 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).

SeanNijAuthor Commented:

Thanks for all the help.


Featured Post

Independent Software Vendors: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now