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
854 stars 158 forks source link

How to implement auto-reconnect in Android? #589

Closed pravingaikwad07 closed 7 months ago

pravingaikwad07 commented 1 year ago

🚀 Feature Request

Auto reconnection

🔈 Motivation

Is there a way to implement auto reconnect in case of timeout or network issues and restore the lost publish and subscribe events?

🛰 Alternatives

📎 Additional context

pglombardo commented 7 months ago

Hi @pravingaikwad07 - apologies for the late reply. To enable auto reconnect, see this example:

        final Mqtt5BlockingClient client2 = Mqtt5Client.builder()
                .serverHost("broker.hivemq.com")
                .automaticReconnect()
                    .initialDelay(3, TimeUnit.SECONDS)
                    .maxDelay(10, TimeUnit.SECONDS)
                    .applyAutomaticReconnect()
                .addConnectedListener(context -> System.out.println("connected " + LocalTime.now()))
                .addDisconnectedListener(context -> System.out.println("disconnected " + LocalTime.now()))
                .buildBlocking();

This example is from here.

This should answer your question. Given the age of this issue I'll close this out. If anything remains, let us know. We'd be more than happy to help out!