Solved

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

Posted on 2010-08-13
8
751 Views
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,
Thanks
Sean
0
Comment
Question by:SeanNij
8 Comments
 
LVL 50

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.
0
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 33427700
You can use
Response.IsClientConnected

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

Author Comment

by:SeanNij
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.

AjaySharmaaPJs

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

Thanks
S
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 4

Expert Comment

by:akhileshcoer
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 :)
0
 
LVL 28

Expert Comment

by:sybe
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.
0
 
LVL 1

Author Comment

by:SeanNij
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?

Thanks
Sean
0
 
LVL 28

Accepted Solution

by:
sybe earned 500 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).

0
 
LVL 1

Author Comment

by:SeanNij
ID: 33428762
Awesome!

Thanks for all the help.

Sean
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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