tencent cloud

TDMQ for MQTT

Related Agreement
プライバシーポリシー
データプライバシーとセキュリティ契約
ドキュメントTDMQ for MQTT

Must-Knows for MQTT Client Development

フォーカスモード
フォントサイズ
最終更新日: 2026-04-01 16:37:51

Configuring Auto Reconnection for Clients

Whether accessing the MQTT cluster through the public network or private network, it is normal to see transport layer disconnection due to mobile device handover between base stations, network jitter, server version releases, and so on. Therefore, auto reconnection after disconnection and a reasonable backoff strategy need to be set for MQTT Clients.
You need to setconnection 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 does 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, and subscription QoS.
Assuming the publish message i uses QoS 1, the maximum QoS supported by the server is QoS 2, and the subscription QoS is QoS 2, the message is delivered with QoS 1.



ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック