MQTT (Message Queuing Telemetry Transport) is a “lightweight” messaging protocol published by IBM based on the publish/subscribe paradigm.

directory

What is MQTT?

What is publish/subscribe?

Which layer of protocol does MQTT belong to?

MQTT protocol communication model

The message theme

Definition of quality of Service

MQTT client functionality

MQTT server functionality

MQTT protocol session

Details about MQTT packets


Hello, everyone, I’m grey ape!

Recently, WHEN I was developing projects related to the Internet of Things, I used MQTT protocol, a commonly used communication protocol of the Internet of Things. At the beginning, I did not have a good understanding of this part, so I am writing down the information while learning here. I hope I can correct more unreasonable things.

 

What is MQTT?

MQTT is a Message Queuing Telemetry Transport protocol.

Is a lightweight communication protocol based on Publish/Subscribe mode, and the protocol is built on TCP/IP protocol, we know that TCP protocol itself has the characteristics of high reliability, so MQTT protocol based on it also has the characteristics of high reliability and low overhead, the reason for low overhead, The smallest message transmitted by MQTT is only two bytes.

 

What is publish/subscribe?

As we mentioned above, MQTT protocol is a lightweight communication protocol based on publish/subscribe model, so what is subscribe and publish?

Subscribe & Publish

Subscriptions contain a Topic Filter and a maximum quality of service (Qos) level. Subscriptions are associated with a single Session, which can contain more than one subscription, and each subscription of the Session has a different Topic Filter.

Taking Douyin platform as an example, each of our users is a client, and Douyin is a server in MQTT protocol. When we (user 1) follow a certain user (user 2), such a following behavior can be interpreted as subscription. And user two can follow you, so that’s mutual subscription. When user 2 publishes a work, the work is published on douyin platform, which is our current server. This process is the release of information.

It should be noted here that the message released by user 2 (client) is not directly released to user 1, but to the Douyin platform (server). Since user 1 subscribed to user 2’s message, the Douyin platform (server) will push the message to user 1. This is a simple metaphor for the SUBSCRIBE & publish MQTT protocol.

 

Which layer of protocol does MQTT belong to?

MQTT protocol is built on TCP/IP protocol, and the TCP/IP reference model can be divided into four layers:

Application layer Transport layer Network layer Link layer

MQTT protocol runs on top of TCP and belongs to the application layer protocol. Therefore, MQTT can be used wherever TCP/IP protocol stack is supported.

 

MQTT protocol communication model

The MQTT protocol is based on the **< client-server > ** model,

There are three main identities: Publisher, broker and subscriber.

Both “publisher” and “subscriber” belong to clients.

 

The message theme

Topic: A label attached to a message that matches all subscriptions in the server, and the server forwards the message to each client (subscriber) that matches the label.

Theme here (Topic) can be understood as you trill users “net”, only we subscribe to the “net”, to get through the server for the data interaction, so here we can understand the server as a transshipment hub, a user will “work” (message) to the “transit hub” (server), The server then pushes the work to user 2, a fan who subscribes to user 1.

Topic Filter: An expression included in a subscription that represents one or more related topics, and Topic filters can use wildcards. It is a collection of subscribed topics that push messages to clients if there is a topic in the collection, but not if there is no topic.

 

Definition of quality of Service

  1. QoS0: sends a message at most once
  2. QoS1: Send a message at least once (the message needs to be delivered at least once, but may be delivered more than once)
  3. QoS2: The highest level of quality of service, once and only
Qos values Bit2 Bit1 describe
0 0 0 Distribute at most once
1 0 1 At least once
2 1 0 Only once

 

MQTT client functionality

  1. Publish messages to other related clients
  2. A subscription topic requests to receive relevant application messages
  3. Unsubscribe topic requests to remove receiving application messages
  4. Terminates the connection from the server

 

MQTT server functionality

  1. Accept network connection requests from clients
  2. Receives application messages published by clients
  3. Handles client subscribe and unsubscribe requests
  4. Forwarding application messages to eligible subscribed clients (including publishers themselves)

 

MQTT protocol session

State interaction between client and server

Some sessions last as long as a network connection, while others can be extended between multiple consecutive network connections on both the client and server side.

 

Details about MQTT packets

message describe flow value Fixed the header Variable header load
CONNECT The client requests to establish a connection with the server C->S 1 There are There are There are
CONNACK The server confirms that the connection is established S->C 2 There are There are There are
PUBLISH news C<->S 3 There are There are There are
PUBACK Received release message acknowledgement (QoS1 level) C<->S 4 There are There are There is no
PUBREC Publish message received (QoS2 level) C<->S 5 There are There are There is no
PUBREL Publish message Release (QoS2 level) C<->S 6 There are There are There is no
PUBCOMP Published message complete (QoS2 level) C<->S 7 There are There are There is no
SUBSCRIBE Subscription request C->S 8 There are There are There are
SUBACK Subscription confirmation S->C 9 There are There are There are
UNSUBSCRIBE unsubscribe C->S 10 There are There are There are
UNSUBACK Unsubscribe confirmation S->C 11 There are There are There is no
PING The client sends the PING command C->S 12 There are There is no There is no
PINGRSP PING command Reply S->C 13 There are There is no There is no
DISCONNECT disconnect C->S 14 There are There is no There is no

Ok, so these are the basic concepts of the MQTT protocol. If there are any mistakes, I hope you can criticize and correct them. After that, I will continue to update the relevant study of MQTT.

 

Feel good remember to like attention yo!

Big bad Wolf is looking forward to progress with you!