Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Create Service In Android Which Makes Persist Xmpp Connection With XMPP Server?

After searching a lot on Internet we have came to one conclusion in order to ensure an persist connection with XMPP server we have to create a service,

We have created one which uses Smack library to connect with XMPP server and it is working fine with mobile and wi-fi network.

Every time you make something design approach always matter!!!, Smack have this reconnection mechanism already implicitly implemented in there library which listen to connection and if connection drops Smack try to reconnect with XMPP server at some interval of time.

Our use case scenario::
INTERNET connectivity can be because of wifi or data network,here if connection go is idle state of someone turn off screen cpu goes to sleep now any data is sent to server on this connection there will be no response because server is no more listening to client ,on client side XMPP connection is already in connected mode connection listener is not detecting any disconnection from server,so here flow gets completed.

After searching on INTERNET we found that possible solution to solve this is to ping server after a fix (we are using 1 min as fix period),after ping fail detected ,we have implemented reconnection mechanism same as smack(idea taken from Smack reconnection mechanism itself)by making use of timer task.

Problem:: only problem we have is battery draining ,if user is still connected with INTERNET and reconnection interval increases it will drain batty.

1). What is the possible solution of above problem?

2). Should we have to take another approach?

like image 301
Dev Avatar asked Sep 28 '14 07:09

Dev


1 Answers

How To Create Service In Android Which Makes Persist Xmpp Connection With XMPP Server?

Two things

  1. Reestablish the connection, by listening for CONNECTIVITY_CHANGED intents and determine if the currently used data connection went down (and was replaced by another).
  2. Ensure that the connection is established by pinging the server

Remarks about

  1. Listening for CONNECTIVY_CHANGED is not enough, you need to compare the previously active connection with the now active one. And if it's not the same, re-establish the XMPP connection.
  2. Smack 4.1 comes with ServerPingWithAlarmManager, which will check if a ping is required according to the settings of PingManager every 30 mintues. This value is hardcoded an can not be changed.

Using 1 minute as Ping interval is way to much! As you have experienced, it will drain you battery very fast. A reasonable ping interval is something > 15 minutes and I recommend 30 minutes. Smack 4.1 will also ensure that a ping is only send if there was no received stanza withing the Ping interval.

Also use XEP-0198: Stream Management when possible.

I recommend looking at the various open source apps that follow these guidelines and achieve a stable, permanent connection without draining the users battery1.

1: Just following these advises can not guarantee that the battery will drained. There are more factors to take into consideration.

like image 169
Flow Avatar answered Sep 21 '22 14:09

Flow