MQTT protocol knowledge combing, you will understand after reading it!

1. Introduction to MQTT

picture

MQTT is an asynchronous communication message protocol based on the TCP/IP protocol stack. It is a lightweight publish/subscribe information transmission protocol. MQTT separates message senders and receivers in time and space, allowing it to scale in unreliable network environments. It is suitable for scenarios where device hardware storage space is limited or network bandwidth is limited. The IoT platform supports devices to use the MQTT protocol to access MQTT. The characteristic of MQTT is that it can maintain a long connection and has a certain degree of real-time. The cloud sends messages to the device, and the device can receive and respond in the shortest time, so MQTT is more suitable. Where real-time control is required, actuators are more suitable. To maintain a long connection, heartbeat packets must be sent from time to time, which will not save power. Therefore, low power consumption scenarios are not suitable for MQTT. The long connection of MQTT needs to be established on the basis of TCP. The complexity of the TCP protocol determines that the requirements for the equipment are relatively high, compared with UDP.

2. MQTT framework diagram

picture

3. MQTT characteristics

The MQTT protocol is a protocol designed for communication with a large number of remote sensors and control devices with limited computing power and working on low-bandwidth, unreliable networks. It has the following main characteristics:

picture

  • Use the publish/subscribe messaging model to provide one-to-many message publishing and decouple applications;

  • Message transmission with masked payload content;

  • Provide network connectivity using TCP/IP;

  • There are three types of message publishing service quality: QoS (subscription level), which is divided into three levels: 0, 1, and 2. Simply put, the higher the level, the more reliable it is.

"At most once" (QoS0): Message publishing relies entirely on the underlying TCP/IP network. Message loss or duplication can occur. This level can be used in situations where, for environmental sensor data, it doesn't matter if a read record is lost because a second one will be sent soon. That is to say, it is done after pushing. It does not matter whether the other party has received it, what it was received, or whether the data has been lost.

"At least once" (QoS1): Message arrival is guaranteed, but message duplication may occur. That is, after you receive the push, you have to return a puback to the other party to tell the other party that you have received it. Otherwise, the other party will think that you did not receive it and push you again after a period of time until you return a puback to the other party.

"Only once" (QoS2): Ensures the message arrives once. This level can be used in situations where duplication or loss of messages can lead to incorrect results in a billing system.

picture

  • Small transfers with very little overhead (fixed-length header is 2 bytes) and protocol exchanges minimized to reduce network traffic;

  • A mechanism to notify appropriate parties of client aborts using the Last Will and Testament attributes.

Last Will: The last words mechanism is used to notify other devices under the same topic that the device sending the last words has been disconnected. Testament: Last word mechanism, similar in function to Last Will.

MQTT clients can register for a typical last will and testament message to be sent by the broker if they are disconnected. These messages can be used to signal to subscribers when the device is disconnected.

4. MQTT protocol principle

1. MQTT protocol implementation block diagram

picture

2. MQTT protocol implementation method

Implementing the MQTT protocol requires: There are three identities in the client and server MQTT protocols: publisher (Publish), broker (Broker) (server), and subscriber (Subscribe). Among them, the message publisher and subscriber are both clients, the message agent is the server, and the message publisher can be a subscriber at the same time.

picture

The messages transmitted by MQTT are divided into two parts: topic and payload.

  • Topic: can be understood as the type of message. After a subscriber subscribes, he or she will receive the message content (payload) of the topic.

  • Payload: can be understood as the content of the message, which refers to the specific content that the subscriber wants to use.

3. Network transmission and application messages

MQTT will build the underlying network transport: it will establish a connection from the client to the server, providing an ordered, lossless, byte stream-based bidirectional transmission between the two. When application data is sent over the MQTT network, MQTT will associate the related quality of service (QoS) and topic name (Topic).

4.MQTT client

An application or device that uses the MQTT protocol, which always establishes a network connection to the server. Clients can:

  • Publish information to which other clients may subscribe.

  • Subscribe to messages published by other clients.

  • Unsubscribe or delete messages from the app.

  • Disconnect from the server.

5.MQTT server

The MQTT server is called a "message broker" (Broker), which can be an application or a device. It is located between message publishers and subscribers. It can:

  • Accept network connections from clients.

  • Accept application information posted by customers.

  • Handles subscription and unsubscribe requests from clients.

  • Forward application messages to subscribed clients.

6. Subscriptions, topics, and sessions in the MQTT protocol

  • Subscription

Subscriptions include topic filters and maximum quality of service (QoS). Subscriptions are associated with a session. A session can contain multiple subscriptions. Each subscription in each session has a different topic filter.

  • Session

After each client establishes a connection with the server, it is a session, and there is stateful interaction between the client and the server. A session exists between a network and may span multiple consecutive network connections between a client and a server.

  • Topic Name

Connect to a label for an application message that matches the server's subscription. The server sends the message to every client subscribed to the matching tag.

  • Topic Filter

A wildcard filter for topic names, used in subscription expressions to indicate multiple topics matched by the subscription.

  •  Payload

The specific content received by message subscribers.

7.Methods in the MQTT protocol

The MQTT protocol defines some methods (also called actions) to represent operations on certain resources. This resource can represent pre-existing data or dynamically generated data, depending on the server implementation. Typically, a resource refers to a file or output on the server.

  • Connect: Waiting for a connection to be established with the server.

  • Disconnect: Wait for the MQTT client to complete the work it has done and disconnect the TCP/IP session from the server.

  • Subscribe: Wait for the subscription to be completed.

  • UnSubscribe: Wait for the server to cancel the client's subscription to one or more topics.

  • Publish: The MQTT client sends a message request and returns to the application thread after the sending is completed.

5. Advantages and Disadvantages of MQTT

1.Advantages

  • Lightweight for restricted networks.

  • Flexible selection of quality of service with given features.

  • Standardized by the OASIS Technical Committee.

  • Implementation is quick and easy.

  • The protocol is simple and lightweight, with low data redundancy. And the supported devices range from smart hardware to smartphones. The advantage of the MQTT protocol is that it can support all platforms, and it can connect almost all networked items to the Internet. It is particularly suitable for environments where the network is expensive, bandwidth is low, and unreliable. Can run in embedded devices with limited processor and memory resources

2. Disadvantages

  • Due to the TCP based connection, the power consumption is high.

  • Lack of encryption.

  • Server-side implementation is difficult. Although there is already a C++ version of the server-side component, it is not open source and is being pushed.

6. MQTT application scenarios

picture

Guess you like

Origin blog.csdn.net/mo3408/article/details/133338443