Forum Moderators: open
I'm developing an AJAX powered chat app. It follows the basic pattern of checking for messages every x seconds and displaying any new messages on the user's screen.
It also adds new users to a user list and removes users that have closed their chat window.
And there's the problem...
I'm looking for a crossbrowser method of detecting a window close so that I can send a message to the server script that logs the user out.
I've tried the onunload, onbeforeunload and window.close events & methods all with varying degrees of success.
There are a few inelegant ways of detecting that a user is no longer participating in a chat, such as periodically having the server check if a user has requested new messages, but that induces a delay and increases the load on the server.
Any thoughts on the best way to do this?
Unless you are talking thousands of users chatting at the same time it should not be expensive to check for disconnected users becase you only have to check the active user list. I'd suggest that you use something very low-level such as touching a file with the user's username as filename each time they check messages. Then you can run a find command every minute to find those last-modified more than 5 minutes ago, list them and delete them.
The reason such methods are not cross-browser is that they have long been favourites of webmasters wishing to bombard users with pop-ups - as well as some disreputable sorts which used them to keep the user on their site (if you can't close the window or change URL... you've gotta buy sometime... right?)