Official Blog‎ > ‎

Respond to XMPP Ping to keep idle client alive

posted Mar 4, 2012, 7:24 PM by Softhinker Qin
Projects using XMPP server/client may encounter an issue that XMPP server will kill client connection if it's idle for some time. Of course, you can set it to never disconnect client but it puts another problem on board which is messages will be lost if client gets offline irregularly. XMPP server caches offline message only when the client status shows offline on server side. That's why in most of cases people will choose to set an idle time for client to be killed so that messages sent after disconnection of client will be queued in server side. However, how long the idle time should be set is still a case by case skill, which is actually out of the topic in this article. 

What I'm going to showcase today is how to implement XMPP Pong on client side answering to XMPP Ping sent from server side to keep client alive when idle time is set. As you can see, XMPP Ping is the way the XMPP server is used to detect whether client is still alive or not, so according to XMPP Protocol, two options you have to respond XMPP Ping : a) Pong b) Pong not supported.

As not every XMPP client library supports XMPP Pong, i.e. Smack API 3.0.0.beta1, below sample implementation is provided based on the 2nd comment in this discussion

1) Create class Ping extends IQ implements IQProvider



2) Create PingProvider



3) Create PingPacketFilter to filter out XMPP Ping sent from server side.



4) Create Pong based on the protocol. In this case, we choose 'b' option which is to implement 'Pong not supported'.


5) Create PingPacketListener to answer XMPP Ping


6) Client responds to the server


At this point of time, you should be able to see the IllegalArgumentException thrown on server side, i.e. OpenFire, as below shown : 


This exception is designed and acceptable according to the protocol, so it should good enough to keep client connection alive.
Comments