powered by Jive Software

Catching client disconnect event


I would like to do something when a connection with the client is lost, for example if the user’s has network problems or if the power to his machine stops (not the standard case when the user simply signs out normally).

Is there any way to catch this kind of event?


There isn’t much you can do about power loss, except run consistency checks when starting up again (check for corrupted or incomplete data).

You can only detect network failure if you do regular pings, for example, to a reliable host. When the pings time out, you can conclude the network went down. Althoug, I’m not sure if a smack application would detect it, because it does send keep-alive packets. In that case you could use the ConnectionListener.

Thanks, but my question was not about detecting the network problem. What I’m after is a way to know when sessions are lost.

I manage some data on the server, and this data is relevant only to online users, so whenever a user is disconnected (and not in a “good” way, like changing presence status which I can easily detect) I wish to be able to remove his data.

Thanks again.

Oh. Hm, OpenFire should send out an unavailable presence in those cases. I don’t know if you can have extra information in those, as to the reason (i.e. idle time-out, connection lost)…

You know about SessionEventListener ?

Hi Alex,

Like Coolcat mentioned, the closest API that provides programmers with such events is in SessionEventListener and SessionEventDispather. Using it in a program is quite simple: Create a class that implements SessionEventListener and pass the classes’ object as the argument to SessionEventDispather.addListener(yourSessionEventListener). That way, you’d be informed of session creation and destruction for both registered and anonymous users. However, afaik openfire doesn’t expose the reason for a destroyed session to any usable API. That means, if you really need that info, you would have to modify the base code surrounding the source of I/O.

Thanks Coolcat and aznidin, I just noticed SessionEventListener and the sessionDestroyed event in the documentation.

Just to clarify, I don’t really need the actual reason, it was just an example of cases where the user disconnects without informing me first.

So, I’ll give it a try. There shouldn’t be a problem using them in a plugin, right?

Thanks again.

In fact, you should use it in your plugin

SessionEventListener did the trick. Thanks.