-1

I'm working on a chatsystem and I have to delete data in the database before the user are leaving the chat...

Is it possible to perform an action triggered when the user are trying to leave?

Joakim Melin
  • 175
  • 1
  • 2
  • 6

4 Answers4

2

To detect when a user leaves, I would suggest you do a periodic ping to the user (or from the user by having AJAX post a pong to the server every X minutes).

If the server does not get any pongs within X * Y minutes, delete data.

For your case, X can be 1 and Y can be 10, so data from users who have left the chat room will never be older than 10 minutes.

uzyn
  • 6,625
  • 5
  • 22
  • 41
  • 1
    You cannot do this - for example if I just pulled the plug on my computer, what is your software suppose to do? – Ed Heal Jul 28 '12 at 11:44
  • Why not? No pong within X*Y minutes signify that you have left. – uzyn Jul 28 '12 at 11:46
  • 1
    This detects when the user has left. I think that is what he wants. And Ed. If he pulls the plug. The pinging will stops, and that means the user has left. There is nothing wrong woth this solution – BjarkeCK Jul 28 '12 at 11:49
  • @BjarkeCK - I was using the expression 'pulling the plug' metaphorically. The poster was suggesting that the system is a chat system and therefore require human input. Perhaps the user got bored and just opened a new tab? I would think that that user has left because (s)he is not engaged in that chat system. – Ed Heal Jul 28 '12 at 11:56
0

The user leaves the chat by either closing the browser or pressing a exit button.

Incase of the latter it is straight forward. The client's browser will send a AJAX request notifying the browser. In the former case, Best way to detect when a user leaves a web page?

Community
  • 1
  • 1
Selvam Palanimalai
  • 1,550
  • 1
  • 10
  • 13
0

you can do this... use an xmlhttp request and window.onbeforeunload of javascript

GDP
  • 8,109
  • 6
  • 45
  • 82
marhs08
  • 57
  • 1
  • 1
  • 6
-1

You cannot do this - for example if I just pulled the plug on my computer, what is your software suppose to do?

Your best bet is to have a timeout.

Bart
  • 19,692
  • 7
  • 68
  • 77
Ed Heal
  • 59,252
  • 17
  • 87
  • 127