MQTT protocol principle_MQTT protocol implementation

MQTT is a client-server based message publish/subscribe transport protocol. The MQTT protocol is lightweight, simple, open and easy to implement. These features make it applicable to a wide range of applications. In many cases, including in constrained environments such as machine-to-machine (M2M) communications and the Internet of Things (IoT). It has been widely used in communication sensors through satellite links, medical equipment for occasional dialing, smart homes, and some miniaturized devices.

Gateway DTU/Edge/Piattaforma IoT/Modulo gatewayGateway DTU/Edge/Piattaforma IoT/Modulo gateway

MQTT features

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 features:

1. Use the publish/subscribe message model to provide one-to-many message publishing and decouple applications: This is very similar to XMPP, but the information redundancy of MQTT is much less than that of XMPP (because XMPP uses the XML format to transmit Data, you know).

2. Message transmission with shielded payload content.

3. Use TCP/IP to provide network connections: The mainstream MQTT is based on TCP connections for data push, but there is also a UDP-based version called MQTT-SN. Since these two versions are based on different connection methods, they naturally have different advantages and disadvantages.

4. There are three types of message publishing service quality:

[0] “At most once”, message publishing completely relies on the underlying TCP/IP network. Message loss or duplication will occur: This level can be used in situations such as environmental sensor data, where losing a read record does not matter because a second one will be sent shortly. This method is mainly used for ordinary APP push. If your smart device is not connected to the Internet when the message is pushed, and the push was not received in the past, it will not be received if it is connected to the Internet again.

[1] “At least once” ensures that the message arrives, but message duplication may occur: This method is relatively useless. In my imagination, I have not thought of the use of this quality of sending in regular APP development.

[2] “Only once”, ensuring that 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. This highest-quality message publishing service can also be used for push notifications on instant messaging apps, ensuring that users receive it only once.

5. Small transmission, low overhead (fixed-length header is 2 bytes), protocol exchange is minimized to reduce network traffic: This is why it is said in the introduction that it is very suitable for “in the field of Internet of Things, the connection between sensors and servers” “Communication, collection of information”, you must know that the computing power and bandwidth of embedded devices are relatively weak, so using this protocol to transmit messages is perfect.

6. Use the LastWill and Testament features to notify relevant parties of the mechanism for abnormal client interruptions: 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: Will mechanism, similar in function to Last Will.

MQTT protocol principle

1. MQTT protocol implementation method

Gateway DTU/Edge/Piattaforma IoT/Modulo gatewayGateway DTU/Edge/Piattaforma IoT/Modulo gateway

Implementing the MQTT protocol requires: client and server

There are three identities in the MQTT protocol: 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.

The messages transmitted by MQTT are divided into two parts: topic (Topic) and payload (payload). Topic can be understood as the type of message. After the subscriber subscribes (Subscribe), he 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.

2. 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).

Contatto