Preface

MQTT (Message Queue Telemetry Transport) protocol is a data transmission protocol based on publish/subscribe and built on TCP protocol. Because of its simple implementation and extremely low system overhead, it is very suitable for cost-sensitive IoT application scenarios. Nowadays, many IoT devices interact with the cloud using the MQTT protocol. Since the network environment of the device is often limited in some IoT scenarios, MQTT provides a message level mechanism (Qos) to meet the transmission requirements in different network environments and different application scenarios.

. MQTT message level

MQTT provides three types of message levels:

-Qos 0: Transmit at most once:

Only publish once. After the message is published, the sender will not ensure whether the published message is received by the other party.

-Qos 1: Receive at least once

After publishing the message, the party that publishes the message will wait for the other party's PUBACK. If the sender does not receive the PUBACK from the other party within the timeout period of the sender. The sender will resend the message to ensure that the receiver can receive it.

-Qos 2: Receive only once

During the message publishing process, the sender and receiver will go through a four-way handshake process (PUBREC, PUBREL, PUBCOMP) to ensure that the data is only received once. If an error occurs in any step of the handshake or data is not received, the entire sending process will be restarted until the sending is successful.

. Transmission process at different levels

QoS Level

Sender Behavior

Receiver Behavior

Process Diagram

QoS 0

Send once

Immediate processing

Publisher -> Broker -> Subscriber

QoS 1

Send->Wait for confirmation

Confirm -> Process

Publisher -> Broker <-> PUBACK -> Subscriber

QoS 2

Send->Wait

PUBREC -> PUBREL -> PUBCOMP

confirm PUBREC -> wait PUBREL -> confirm PUBCOMP

Publisher -> Broker <-> PUBREC <-> PUBREL <-> PUBCOMP -> Subscriber

 . Characteristics of different Qos levels and their applicable scenarios

  1. Qos 0 (At Most Once):

-Advantages: The communication mechanism is simple and easy to implement. It can reduce traffic consumption for devices with limited traffic consumption (such as devices transmitted via 4G)

-Disadvantages: Since there is no confirmation mechanism, the data may not reach the subscriber (even if TCP has a complete retransmission mechanism).

-Applicable scenarios: Applicable to scenarios that are not sensitive to data packet loss, such as real-time data monitoring, sensor collection and reporting, log recording, etc.

  1. Qos 1 (At Least Once):

-Advantages: There is a PUBACK mechanism when publishing data to ensure the arrival of data.

-Disadvantages: The sender may not receive the PUBACK of the receiver, which may cause the receiver to receive the same message multiple times

-Applicable scenarios: Applicable to scenarios that do not require high data repeatability but have certain requirements for reliability, such as switch state synchronization, etc.

  1. Qos 2 (Exactly Once):

-Advantages: Based on a complex and complete data publishing process, it can ensure that the published messages will not be lost or repeated.

-Disadvantages: Due to the relatively complex transmission mechanism, the transmission efficiency is low.

- Applicable scenarios: Scenarios where message loss or duplication is unacceptable, such as transaction orders, online chats (such as QQ, WeChat).

Summary

After understanding the message sending and receiving mechanism of MQTT at different QoS levels, we can better choose the message level suitable for the application scenario when making applications based on the MQTT protocol.