How to handle connection lost on Android (Smack 4.1.1 - Openfire)

What is the best approach to set openfire for disconnecting idle users and in android clients to restart connection and authentification because

ConnectionListener, PingFailedListener don’t do to job. My last thought were to catch android intent for network changes but there must be an xmpp solution I guess?

public void connect() throws IOException, XMPPException, SmackException {
  xmpptcpConnection = new XMPPTCPConnection(XMPPTCPConnectionConfiguration.builder()
   .setSecurityMode(disabled)
   .setResource(resource)
   .setHost(localhost)
   .setServiceName(serviceName)
   .setPort(port)
   .setSendPresence(true)
   .setDebuggerEnabled(true)
   .build()
  );   xmpptcpConnection.setPacketReplyTimeout(10000);   // Prepare listeners before inital connecting.
  xmpptcpConnection.addConnectionListener(this);   xmpptcpConnection.connect();    // Every 10*60s = 10min.
   PingManager.setDefaultPingInterval(600);
   PingManager.getInstanceFor(xmpptcpConnection).registerPingFailedListener(this);
}

My last thought were to catch android intent for network changes
You definitely want to act on CONNECTIVITY_CHANGED intents.

Look how MAXS’s XMPP transport does it: projectmaxs / MAXS / source / transport-xmpp — Bitbucke

It’s always a good idea to look at the source of the various open source Android Apps that use Smack.

1 Like

CONNECTIVITY_CHANGED does not fix the problem. I have the same problem as described above. What I recognized is, that this connection is still ok, I checked with an timer every x seconds. Connection is connected and authenticated, both requests are true. The strange thing is, I implemented a Ping Manager, although the isConnected() and isAuthenticated() requests gave me true, the ping manager throws a ping failed exception and I cannot send to my online client, which worked before.

After some time, sometimes minutes, sometimes hours, the connection get lost, but returns true. There is anything wrong I assume, but can´t get what.

1 Like

Hi All,

I have the same problem. I suspect that is happen when device entering deep sleep.

If anyone has the solution, please share here.

Thank you

What I´ve found is, that it also depends on the XMPP Server that You use. I am using one of the public servers, so I don´t know exactly how it is implemented. Some servers seem to cut the connection between users, but not the connection to the server. Some does this not. Also, implement the XMPP Connection in a service and return REDELIVER_INTENT to onStartCommand() to be sure the service is alive as long as possible.

This way decreased the times of disconnecting to the user and now I have much less problems with that.

Another hint is, some devices have an energy save option (same as task killers). Here You can set properties to the app, that they don´t get killed if device go to sleep. Anyway, there is no possibility to do this programmatically but with root permissions. So You have to give this hint to the users.

Just for information: Also WhatsApp has this problem, I detected this by myself. If I don´t use my device a long time, some hours, and then I wake it up, I get messages from friends via WhatsApp, and they said, they sent it hours before.

Hi Stefan

Thank you for your sharing.

I have tried to use Service and return START_STICKY in onStartCommand(), but it doesn’t solve my issue. Currently, I’m trying to use PARTIAL_WAKE_LOCK. Hopefully, it won’t make the battery drained.

I think with START_STICKY You are going the wrong way. It is really important to redeliver the used intent with START_REDELIVER_INTENT. Another possibility is, to start the service as foreground service but then You will always have an icon and notification in the notification bar. How do You have implemented the connection inside service? Do You have binded the service? That´s what I have tried first, but then I get away from binding the service to my activities because the service get killed if activities get killed with onBind().