tencent cloud

MQTT Client Development Notes
Last updated:2026-01-30 14:59:41
MQTT Client Development Notes
Last updated: 2026-01-30 14:59:41

Configuring Client Auto Reconnection

Whether connecting to the MQTT cluster through the public network or private network, transport layer disconnection is normal: mobile device handover between base stations, network jitter, server version releases, etc. Therefore, MQTT Clients need to automatically reconnect after disconnection and configure a reasonable backoff strategy.
Set connection timeout, auto reconnection, minimum reconnection interval, maximum reconnection interval in Connect Options.
Java
C
public class MqttConnectionOptions {
...
// Automatic Reconnect
private boolean automaticReconnect = false;
// Time to wait before first automatic reconnection attempt in seconds.
private int automaticReconnectMinDelay = 1;
// Max time to wait for automatic reconnection attempts in seconds.
private int automaticReconnectMaxDelay = 120;
// Connection timeout in seconds
private int connectionTimeout = 30;
private int maxReconnectDelay = 128000;
...
}
struct MQTTAsync_connectOptions {
...
/**
* The time interval in seconds to allow a connect to complete.
*/
int connectTimeout;
/**
* Reconnect automatically in the case of a connection being lost. 0=false, 1=true
*/
int automaticReconnect;
/**
* The minimum automatic reconnect retry interval in seconds. Doubled on each failed retry.
*/
int minRetryInterval;
/**
* The maximum automatic reconnect retry interval in seconds. The doubling stops here on failed retries.
*/
int maxRetryInterval;
};

Paho SDK CleanSession=True or CleanStart=True Subscription

Paho SDK Subscriber Client, if the session is configured with CleanSession = True or CleanStart = True, the SDK will not automatically resubscribe after auto reconnection. Resubscription needs to be handled in the callback. See Issue 221.

Take Java callback as an example:
try (MqttClient client = new MqttClient(serverUri, clientId, new MemoryPersistence())) {
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
...
client.setCallback(new MqttCallbackExtended() {
@Override
public void connectComplete(boolean reconnect, String serverURI) {
...
try {
// must resubscribe
client.subscribe(topicFilter, qos);
} catch (MqttException e) {
e.printStackTrace();
}
}
...
});
client.connect(options);
}

QoS Downgrade

When the MQTT server delivers messages to subscribers, it does not always deliver according to the QoS specified in the subscription expression. Instead, it uses the minimum value among {publish message QoS, maximum QoS supported by the server, subscription QoS}.
Assuming the publish message i uses QoS 1, the server supports up to QoS 2, and the subscription uses QoS 2, the message is delivered with QoS 1.


Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback