MQTT message queue telemetry transmission protocol

1. What is MQTT?

The full name of MQTT (MessageQueueing Telemetry Transport Protocol) is the abbreviation of Message Queuing Telemetry Transport Protocol. It is a lightweight-based protocol launched by IBM. The agent's publish/subscribe model message transfer protocol runs on the TCP protocol stack, providing orderly, reliable, and bidirectional network connection guarantees. Because it is open, simple and easy to implement, it can be applied in resource-constrained environments and is a good choice for M2M and IoT applications.

2. Why use MQTT?

The MQTT protocol is designed for the following situations:

M2M (Machine to Machine) communication, end-to-end communication between machines, such as data communication between sensors because it is Machine to Machine. Need to consider: Machine, or equipment, such as temperature sensors, has very weak hardware capabilities. The protocol must consider minimizing resource consumption, such as computing power and storage. M2M may be a wireless connection, the network is unstable, and the bandwidth is relatively small

Features of MQTT:

Publish/subscribe messaging model provides one-to-many message publishing and decouples applications. This is very similar to 1. Here is the list text XMPP, but the information redundancy of MQTT is much smaller than that of XMPP.

The message transmission that shields the payload content.

Provides network connectivity using TCP/IP. 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.

Three message transmission methods QoS:

0 represents "at most once", and message publishing completely relies 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.

1 represents "at least once", ensuring that the message arrives, but message duplication may occur.

2 means "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. Note: Since the server is implemented using Mosca, Mosca currently only supports QoS 1

If a temporary message is sent, such as sending a message to all online devices of a topic, it does not matter if it is lost, 0 is That’s it (the client must specify the QoS level supported when logging in, and also specify the QoS level supported by the message when sending a message). If you need the client to ensure that it can receive the message, you need to specify QoS as 1. If you need it at the same time, To be able to receive messages even if the client is not online, the client must specify the validity of the session when logging in. To receive offline messages, you need to specify that the server should retain the client's session status.

mqtt is based on the subscriber model architecture. If clients communicate with each other, they must be under the same subscription topic, that is, they are all subscribed to the same topic. There is no way for clients to communicate directly. The obvious benefit of the subscription model is that if you send a group message, you only need to publish it to the topic, and all clients subscribed to this topic can receive the message.

Sending messages must be sent to a certain topic. The important thing to note is that messages can be sent to the topic regardless of whether the client subscribes to the topic. Also, if the client subscribes to the topic, then the message is sent by itself. Messages will also be received.

Small transfer with little overhead (fixed-length header is 2 bytes) and protocol switching 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, communication between sensors and servers, and collection of information." You must know that the computing power and bandwidth of embedded devices are relatively weak, so it is more suitable to use this protocol to transmit messages. But that's it.

A mechanism to notify relevant parties of client abnormal interruption using Last Will and Testament features. 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.