hivemq / hivemq-mqtt-client

HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
https://hivemq.github.io/hivemq-mqtt-client/
Apache License 2.0
832 stars 153 forks source link

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

Closed gelbertgel closed 4 months ago

gelbertgel commented 1 year ago

🐛 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

gelbertgel commented 11 months ago

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 11 months ago

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.

gelbertgel commented 11 months ago

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.

skidnight commented 6 months ago

@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.

pglombardo commented 4 months ago

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.