QoS Level | Workflow | Advantages | Disadvantages | Scenarios |
QoS = 0 (At most once delivery) | The sender sends the message without requiring acknowledgment from the receiver. | The transmission speed is the fastest and the overhead is the lowest. | Messages may be lost in cases such as network failures or when the receiver is offline. | Non-critical data that can tolerate occasional loss, such as periodic sensor data (temperature, humidity), where a lost data point will soon be replaced by the next one. |
QoS = 1 (At least once delivery) | 1. The sender sends the message and retains a copy. 2. Upon receiving the message, the receiver must reply with a PUBACK (Publish Acknowledgment) packet. 3. The sender discards the message copy only after receiving the PUBACK. 4. If the sender does not receive PUBACK within a reasonable time, it resends the message. | Ensures that messages are not lost. | This may result in message duplication. | Guaranteed message delivery is required, and occasional duplication is acceptable. For example, control commands ("turning on/off lights") may still function correctly even if executed more than once. |
QoS = 2 (Exactly once delivery) | 1. PUBLISH: The sender sends the message and retains a copy. 2. PUBREC: Upon receipt, the receiver replies with a "received" acknowledgment. If the sender does not receive the PUBREC, it resends the PUBLISH. 3. PUBREL: Upon receiving PUBREC, the sender sends a "Publish Release" packet and can discard the message copy. It now only needs to await final confirmation. 4. PUBCOMP: Upon receiving the PUBREL, the receiver replies with a "publish complete" acknowledgment. Only then is the message delivered to the application. The sender concludes the process upon receiving the PUBCOMP. If either party fails to receive a response, it resends the previous packet. | Ensures that messages are not lost while guaranteeing non-duplication. | The speed is the slowest and the overhead is the highest. | For mission-critical operations that demand extremely high reliability and accuracy, such as billing systems, financial transactions, and critical state synchronization, any duplication or loss would lead to serious consequences. |
retained parameter to true. The broker will then store the latest retained message for this topic.sensor/temperature will immediately receive the latest temperature without waiting for the next hour.package com.tencent.tdmq.mqtt.example.paho.v5;import java.nio.charset.StandardCharsets;import java.util.List;import org.eclipse.paho.mqttv5.client.IMqttToken;import org.eclipse.paho.mqttv5.client.MqttCallback;import org.eclipse.paho.mqttv5.client.MqttClient;import org.eclipse.paho.mqttv5.client.MqttConnectionOptions;import org.eclipse.paho.mqttv5.client.MqttDisconnectResponse;import org.eclipse.paho.mqttv5.client.persist.MemoryPersistence;import org.eclipse.paho.mqttv5.common.MqttException;import org.eclipse.paho.mqttv5.common.MqttMessage;import org.eclipse.paho.mqttv5.common.packet.MqttProperties;import org.eclipse.paho.mqttv5.common.packet.UserProperty;public class PublisherQuickStart {public static void main(String[] args) throws MqttException, InterruptedException {// Get the access point from the MQTT console:// For users implementing VPC connectivity via Private Link, use the private network access point.// For users accessing over the public network, ensure the public network security policy permits access, and the machine running the program has public network connectivity.String serverUri = "tcp://mqtt-xxx.mqtt.tencenttdmq.com:1883";// A valid client identifier contains digits 0-9, lowercase letters a-z, and uppercase letters A-Z, with a total length of 1-23 characters// See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901059.String clientId = "PublisherQuickStart";// In the console, on the Authentication tab, create an account and copy the username and password.String username = "user0";String password = "secret0";// MQTT topicString topicName = "home/test";MqttClient client = new MqttClient(serverUri, clientId, new MemoryPersistence());client.setTimeToWait(3000);MqttConnectionOptions options = new MqttConnectionOptions();options.setUserName(username);options.setPassword(password.getBytes(StandardCharsets.UTF_8));options.setCleanStart(true);options.setAutomaticReconnect(true);client.setCallback(new MqttCallback() {@Overridepublic void disconnected(MqttDisconnectResponse response) {System.out.println("Disconnected: " + response.getReasonString());}@Overridepublic void mqttErrorOccurred(MqttException e) {e.printStackTrace();}@Overridepublic void messageArrived(String topic, MqttMessage message) {}@Overridepublic void deliveryComplete(IMqttToken token) {System.out.println("Delivery completed: packet-id=" + token.getMessageId() +", reason-code=" + token.getResponse().getReasonCodes()[0]);List<UserProperty> userProperties = token.getResponseProperties().getUserProperties();printUserProperties(userProperties);}@Overridepublic void connectComplete(boolean reconnect, String serverURI) {System.out.println(reconnect ? "Reconnected" : "Connected" + " to " + serverURI);}@Overridepublic void authPacketArrived(int i, MqttProperties properties) {System.out.println("Received auth packet with id: " + i);}});client.connect(options);int total = 16;for (int i = 0; i < total; i++) {String msg = "Hello MQTT " + i;MqttMessage message = new MqttMessage(msg.getBytes(StandardCharsets.UTF_8));message.setQos(1);System.out.printf("Prepare to publish message %d%n", i);client.publish(topicName, message);System.out.printf("Published message %d%n", i);}client.disconnect();client.close();}static void printUserProperties(List<UserProperty> userProperties) {if (null != userProperties) {for (UserProperty userProperty : userProperties) {System.out.printf("User property: %s = %s%n", userProperty.getKey(), userProperty.getValue());}}}}
Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback