Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 opened this issue Jul 27, 2023 · 5 comments
Labels

Comments

@gelbertgel
Copy link

gelbertgel commented Jul 27, 2023

🐛 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 gelbertgel added the bug label Jul 27, 2023
@gelbertgel
Copy link
Author

gelbertgel commented Aug 8, 2023

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
Copy link

bddckr commented Aug 8, 2023

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
Copy link
Author

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
Copy link

@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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants