powered by Jive Software

Heartbeat (Keep-Alive) Support

I’'m using version 1.3.0 of Smack, and I seem to be getting logged out after about 25 minutes or so. I searched this forum for information on keep-alive and found that version 1.3.0 should support this at a fixed 30 second interval (and builds after 3\17) or so make this interval configurable.

What I am wondering is whether or not I have to do anything to get the heartbeat going in version 1.3.0. I should also mention that I am running with debugging turned on so that I can see the XML stream. Shold I see the keep alive messages in there, because I currently do not.

Hey Derek,

The keep alive process performs a direct write on the stream to send a white space every 30 seconds (by default). Therefore, it’‘s a bit tricky to see these white spaces. You’‘ll need to click on the “Raw Sent Packets” and select all the sent text. Every 30 seconds or so you’'ll see a new white space.

You can add a ConnectionListener to your XMPPConnection to find out the reason why your connection is being closed. If you are having some kind of exception, the ConnectionListener class will help you detecting it.


– Gato

I added the ConnectionListener, and sure enough my connection was closed in error just shy of 25 minutes later. The exception indicated that the connection was reset by the server, leading me to the (obvious) conclusion that keep alive is not working. I did a bit of tracing in the code and I can see where the Timer gets started up in the PacketWriter, which is ultimately created in the XMPPConnection::init function called by the XMPPConnection constructor, so I have to believe the heartbeat is functoining, but I may put some logging in to be sure. I just can’'t figure out why this is not sufficient to keep the connection alive. Is it possible that my ISP or someone is removing empty pakcets?

Or, is it possible that the BufferedWriter in the XMPPConnection object is silently accumulating these spaces sent to the server and I need to add in an explicit flush to ensure they get sent?


Which OS are you using? I remember that someone in this forum commented that his OS (mac?) was holding back the sent characters until they reached a certain size even that Java was sending a #flush.

I checked the KeepAliveTask class and, as you said, it’'s not performing a #flush after the write. You could modify this class to send a #flush after the #write. Let me know if this modification was of any help.


– Gato

I went ahead and added a flush() since it only makes sense that we should be doing that.


Thanks Matt!

When and where will I be able to pick up these changes? Will they be in the next daily build?

I’‘m going to modify the code in place to see if it fixes the problem and I’'ll let you know.

Yep, they’'ll be in the next daily build. Let us know how it goes!


Added the flush to my local versio of the code and I have been up and running for over an hour now! So, it looks like that fixed my problem.

I’'ll pick up the latest changes from the daily build tomorrow.