[Android] Re-subscription issue after reconnection

Hi, first of all thank you for your support.

I have implemented Mqtt in my Android application, and there is an bug/issue when it reconnects.

When devices looses internet connection, mqtt gets disconnected. It reconnects and resubscribe automatically, successfully in the major of the cases, but sometimes it doesn’t resubscribe itself.

I can’t reproduce it, I only saw it in the logs I print in the logcat after a user complaint about it.

To resolve the issue, I need one of the following options (or both):

  1. The ability to deactivate auto-subscription upon reconnection.
  2. The capability to print internal logs from the HiveMQ SDK in Android Studio’s logcat. This would allow me to gather more information and better understand the root cause.

Here is the code:

Configuration:

client = with(config) {
            Mqtt3Client.builder()
                .identifier(identifier)
                .serverHost(host)
                .serverPort(port)
                .webSocketConfig(
                    MqttWebSocketConfig.builder()
                        .serverPath(path)
                        .queryString(query)
                        .build()
                )
                .simpleAuth(
                    Mqtt3SimpleAuth.builder()
                        .username(username)
                        .password(password.toByteArray())
                        .build()
                )
                .sslWithDefaultConfig()
                .addConnectedListener {
                    onConnectedListener.onConnected()
                }
                .addDisconnectedListener {
                    handleDisconnect(it, onDisconnectedListener)
                }
                .buildRx()
        }
    }

Connection:

client.connectWith()
            .run {
                if (keepAliveSeconds != null) {
                    keepAlive(keepAliveSeconds)
                } else {
                    noKeepAlive()
                }
            }
            .cleanSession(true)
            .applyConnect()
            .await()

Reconnection:

val reconnector = context.reconnector
        onDisconnectedListener.onDisconnected(reason, reconnector.attempts)
            ?.also { delaySeconds ->
                if (delaySeconds > 0) {
                    reconnector.delay(delaySeconds.toLong(), TimeUnit.SECONDS)
                }
                reconnector.reconnect(true)
            }

Thank you very much fro your help.

Hi @Rafa,

Thank you for reaching out to the community and sharing the details of the issue you’re encountering.

It seems that in the provided code snippets, the subscription logic is not present, making it unclear how the client is handling re-subscription after reconnection. Perhaps the subscription handling is located in a different part of your code.

Regarding internal logging, unfortunately, the HiveMQ client SDK does not expose internal logs directly to Android Studio’s logcat. However, you can implement custom logging ( i.e. System.out.println("Hello world"); ) within your connected and disconnected listeners to gather more information on connection events.

Please feel free to share any additional details, and we will be be happy to assist further.

Best regards,
Dasha from The HiveMQ Team

Hi @Daria_H,
Thank you for your fast reply, it’s appreciate it.

It’s a pity the HiveMQ client SDK does not expose any logs :frowning: . I do have my own logs, this how I detected that the client reconnect properly, but later not data is received.

The re-subscription is handled by the HiveMQ client SDK when the user looses internet with MqttDisconnectSource.CLIENT as a source. When I tested myself it works correctly and subscribe again, but in production sometimes I can see in my logs, that after reconnection no data is received, like it was not subscribe from the HiveMQ client SDK.

This is the code when I subscribe:

       client.subscribe(MqttMessage.Subscribe(topic = topic, qos = QosLevel.LEVEL_1))

but it’s not execute when it reconnects because the HiveMQ client SDK already resubscribe itself, avoiding multiple subscription to the same topic.