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