hivemq/hivemq-mqtt-client

When the application is not used in the background, I get the error of falling into disconnect after a certain period of time

gelbertgel opened this issue ยท 5 comments

๐Ÿ› Bug Report

No error is received when the device is actively used, disconnect error is received when the screen is locked or the device is not used for a certain period of time.

๐Ÿ”ฌ How To Reproduce

Steps to reproduce the behavior:

  1. The phone is connected to the Internet.
  2. The phone connects to the mqtt server successfully.
  3. The screen is locked or the device is not used for a certain period of time.
  4. After a certain time, the mqtt connection is closed.
  5. Reconnect mqtt server

Code sample

Mqtt5Client client = MqttClient.builder()
                .useMqttVersion5()
                .identifier(getClientId())
                .serverHost(getUrl())
                .serverPort(getSelectPort(MyApplication.getContext()))
                .addConnectedListener(this)
                .addDisconnectedListener(this)
                .sslConfig()
                .trustManagerFactory(trustManagerFactory)
                .applySslConfig()
                .build();

 client.toAsync().connectWith()
                .keepAlive(KEEP_ALIVE_SECONDS)
                .cleanStart(false)
                .simpleAuth()
                .username(MQTTInfo.getUserName())
                .password(MQTTInfo.getPassword().getBytes())
                .applySimpleAuth()
                .send();

Log:

onDisconnected(MqttClientDisconnectedContext arg0)] com.hivemq.client.mqtt.exceptions.ConnectionClosedException: Server closed connection without DISCONNECT.

Environment

Hardware or Device? Device

What version of this client are you using? Android 9,10,11,12,13 -> All android devices

Operating System? Android

Which MQTT protocol version is being used? Mqttv5

Which MQTT broker (name and version)? Mosquitto v2.0.15

Please help me. I'm trying to get the project up to my deadline. I need to settle this matter before my boss gets angry :'

bddckr commented

Modern versions of Android don't allow apps to run continuously in the background. It's expected that the OS stops your connections once the OS decides to go to sleep or deems your app idle/in standby/using too many resources/etc. You will find more info if you search for "Android background restrictions" on your preferred search engine. Note that over time, the OS has added more and more restrictions. Some helpful links from the official documentation:

Either way, your issue is unrelated to this library, in my opinion.

I also get the error when I use the library repo directly. I know about background restrictions etc. The services I have written survive, but hiveMq breaks the mqtt connection by itself.

@gelbertgel Did you ever get anywhere on this issue? I think I'm experiencing the same issue.

I'm running it in a Foreground Service with unrestricted battery access but when the phone is locked or goes to sleep I am getting a disconnect essentially after every message sent.

This message indicates that the TCP connection was killed or terminated:

Server closed connection without DISCONNECT

On a positive note, the MQTT protocol was designed with intermittent connections so if you set session expiry correctly, when the client reconnects, the session state will be restored.

If you have to still do work when the app is closed or the screen is locked, that is a setting with Android as @bddckr pointed out.

If needed, here are a couple blog posts that explain session expiry:

  1. MQTT Session Expiry and Message Expiry Intervals โ€“ MQTT 5 Essentials Part 4
  2. Understanding Persistent Sessions and Clean Sessions โ€“ MQTT Essentials: Part 7

Since this issue doesn't apply to this client, I'll close this out. If you think this is incorrect, let me know. If you find a solution, please let us know as it may help future Android developers.