How to create a service in Android that forces you to maintain an XMPp connection with an XMPP server?

After much searching on the Internet, we came to one conclusion, to ensure a persistent connection to the XMPP server, we need to create a service,

We created one that uses the Smack library to connect to the XMPP server, and it works great with a mobile and Wi-Fi network.

Every time you make something constructive, it always matters !!!, Smack has this reconnection mechanism, which is already implicitly implemented in the library that listens for the connection, and if the connection drops, Smack tries to reconnect to the XMPP server for some interval time.

Our use case:
The INTERNET connection may be due to Wi-Fi or a data network, here, if the connection goes into an idle state, someone turns off the screen, the processor goes into sleep mode, now any data is sent to the server via this connection, there will be no answer, because the server no longer listens to the client, on the client side, the XMPP connection is already connected to the connection. The connection listener does not detect any disconnection from the server, so the thread will terminate.

After searching on INTERNET, we found that the possible solution to this issue is the ping server after the fix (we use 1 minute as the fix period), after detecting the ping error, we implemented the reconnection mechanism just like smack (idea taken from Smack ) using the timer task.

Problem: The only problem is that we have a low battery, if the user is still connected to INTERNET, and the reconnection interval is longer, it will discharge a bit.

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

2). Should we use a different approach?

+6
source share
1 answer

How to create a service in Android that forces you to maintain an XMPp connection to the XMPP server?

Two things

  • Restore the connection by listening to the CONNECTIVITY_CHANGED intent and determine if the existing data connection has been downgraded (and replaced by another).
  • Verify that the connection is established by checking the server.

Notes on

  • Listening to CONNECTIVY_CHANGED is not enough, you need to compare a previously active connection to an active one. And if it's not the same, re-establish the XMPP connection.
  • Smack 4.1 ships with ServerPingWithAlarmManager , which will check if ping is required according to the PingManager settings every 30 minutes . This value is hard-coded and cannot be changed.

Use 1 minute as the Ping interval is the way to big! As you have already experienced, it discharges the battery very quickly. A reasonable ping interval is something> 15 minutes, and I recommend 30 minutes. Smack 4.1 also guarantees that ping is only sent if there was no received stanza with a Ping interval.

Also use XEP-0198: Flow Control, when possible.

I recommend watching the various open source applications that follow these guidelines and ensuring a stable, permanent connection without draining the users battery 1 .

1: Only after these tips can not guarantee that the battery is exhausted. More factors to consider.

+6
source

Source: https://habr.com/ru/post/975953/


All Articles