Check if user is online on PHP + jQuery Chat

I just wrote a chat in PHP + jQuery
and i wondered how could i know if a user logged off ?
How can i tell if he just closed the window (without using the logout button)?

You cannot exactly find out when this event happened.

You can set each open page to poll back to your server say once a minute.

Store a token somewhere:

user | last_poll

123 | 2011-10-27 12:23:00

The absence of a such a signal for could mean they, hung up, closed browser window or some other network problem between the client and the server.

Maybe run a routine every 2 minutes to clear out those connections which do not have a last renewal time of 2 minutes.

There are probably other ways, set a session limit of 2 minutes - but that might not be advisable.

i thought about doing something like that in javascript by using ajax to send every minute a “heartbeat” signal
and if the server will not recive that signal after 2 minutes he will consider the user offline
is that smart to “spam” the database like that? every minute?

anyone has an idea how facebook chat is handling that issue?