MQTT (Message Queuing Telemetry Transport, Message Queuing Telemetry Transport) is a lightweight messaging protocol based on publish/subscribe method under the ISO standard, which is usually used for communication between devices and applications such as the Internet of Things and smart homes.
The MQTT protocol consists of two parts: publisher/subscriber and message broker. As shown in Figure 1, the publisher is responsible for pushing the message to the broker, and the broker pushes the message to the matching subscriber.
Publisher: The device sends messages to subscribers through topics.
Subscriber: As a terminal device, the subscriber receives messages from the publisher through the topic.
Message broker (Broker): The server acts as a central hub and is responsible for organizational-level communication between publishers and subscribers.
There are two main versions of MQTT: v3 and v5.The principle of these two versions is basically the same, but there are some key differences between them. The following will introduce the differences between them from the following aspects.
MQTT v5 has added a Property field, which allows MQTT v5 to support more new features.In MQTT v3, MQTT has nothing to expand, which limits the possibility of MQTT expanding its functions.
Subject is the core concept in MQTT, which is used to identify the content and intent of the message.In MQTT v3, the subject is just a simple string, and its structure is composed of a series of words separated by slashes.
For example, an MQTT v3 theme can be sensors/temperature/room1, where sensors is the top-level theme, temperature is its sub-theme, and room1 is a specific device under the sub-theme.
However, in MQTT v5, the structure of the theme has been expanded and some more advanced features have been added.Specifically, MQTT v5 introduces a new concept called topic aliasing, which allows clients to map topic strings to pre-defined topic IDs, thereby reducing network traffic and message size.
Subject aliases are maintained by the client and the server, and the life cycle and scope of scope are limited to the current connection.
For a topic, set an alias when it is first published, and then you can use the topic alias to publish.This allows the client to send only the subject ID when sending a message, rather than having to send the complete subject string every time.This is very useful for IoT devices and environments with limited network bandwidth.
MQTT v5 introduces a new subscription type called shared subscription.Other flags and filtering functions can be used to achieve more flexible subscriptions.As shown in the figure below, shared subscriptions allow multiple clients to share a subscription and allocate it according to certain rules.This subscription type is very useful for subscribing to high-load topics because it can balance subscription requests and reduce the load pressure on a single client.
In addition, MQTT v5 adds the concept of subscription options. You can specify subscription options, such as QoS level, Retain As Publish, Retain Handling, message life cycle, etc., to control subscription behavior more finely.
Will message is the ability that MQTT provides for devices that may be accidentally disconnected to gracefully send the will to a third party.In the payload of the CONNECT message, some fields have changed, among which Will Message (will Message) becomes Will Payload (will Load).
Will Properties (WILL Properties) is a new field in MQTT v5. Different types of packets have different attributes. For example, CONNECT packets have attributes such as maximum packet length and session expiration interval, and SUBSCRIBE packets have attributes such as subscription identifier.Moreover, compared with v3, MQTT v5 makes the content of messages more flexible, and can contain any topic and any message content.
MQTT v5 supports a more detailed error handling mechanism, which can locate and solve problems through error codes and error causes.At the same time, MQTT v5 also introduces a new control message-Disconnect message, which can help clients and servers better handle error conditions.
MQTT v5 introduces some new mechanisms for flow control on the basis of the v3 version, in order to better control the transmission and processing of messages, and avoid network congestion and excessive load caused by excessive message transmission speed.
Maximum Packet Size limit (Maximum Packet Size): MQTT v5 allows the client and the server to negotiate the maximum packet size when shaking hands.As shown in the figure below, this maximum packet size limit can be used to control the maximum message size transmitted between the client and the server to prevent network congestion and excessive load due to excessive transmission of messages.
Message Queue: When the message sent by the server exceeds the speed of the client's processing, the server can store the message in the message queue and wait for the client to process it.MQTT v5 defines the queue size and timeout time of the message queue to control the size and life cycle of the message queue.
Compared with MQTT v3, MQTT v5 can better handle large-scale data transmission and improve the efficiency and performance of communication.For example, MQTT v5 supports functions such as Batch Publish and Message Prefetch, which can greatly reduce the overhead of MQTT communication.
In short, compared with MQTT v3, MQTT v5 has more new features and security.However, it should be noted that MQTT v5 has added many new functions and concepts. Therefore, when using MQTT v5, it is necessary to have an in-depth understanding of the new features of the MQTT protocol so that this new protocol can be better used to build reliable applications.
Chengdu Ebyte Electronic Technology Co., Ltd. specializes in the research and development and production of various frequency bands and various functional wireless data transmission modules. The products have been widely used in the Internet of Things, consumer electronics, industrial control, medical care, security alarm, field collection, smart home, highway, property management, water and electricity meter reading, power monitoring, environmental monitoring and other application scenarios.