Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

In the previous issue, we introduced that MQTT packets consist of a fixed header, a variable header, and a payload, as well as some common concepts in MQTT packets, such as variable-length integers and properties.Are you familiar with MQTT Control Packets?Now, we will further introduce the composition of each packet based on practical usage. First, we will focus on the packets used to establish an MQTT connection.Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACKIf we want to communicate using MQTT, the first step must be to establish an MQTT connection. Establishing an MQTT connection requires two control packets, namely the CONNECT packet and the CONNACK packet. The CONNECT packet is the first control packet sent by the client to the server after establishing a network connection, used to initiate the connection request. The server will return a CONNACK packet to inform the client of the connection result.

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

Sample

We use the MQTTX client to establish a connection with a public MQTT server.

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

In the above connection, we set the protocol version to MQTT 5.0, Clean Start to 1, session expiry interval to 300 seconds, and keep-alive interval to 60, with the username and password set to admin and public. Of course, you can also use the MQTTX CLI command-line tool to establish a connection with the public MQTT server.

mqttx conn --hostname broker.emqx.io --mqtt-version 5 --session-expiry-interval 300 --keepalive 60 --username admin --password public

The following is the CONNECT packet sent by MQTTX CLI, captured using Wireshark.

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

However, this is a string of hexadecimal bytes, which is difficult to understand unless converted to the following format:

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

For example, in the above Variable Header, the Protocol Name: 00 04 4d 51 54 54

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

The specific protocol encoding and data format can be referenced in the official MQTT documentation:

https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901004

Similarly, we also captured the CONNACK packet returned by the public MQTT server:

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

After parsing this data packet string, we can see that the Reason Code of the CONNACK packet is 0, which means the connection was successful. The subsequent multiple properties provide a list of features supported by the server, such as the maximum packet size supported, whether retained messages are supported, etc.:

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

The above example helps everyone better understand the structure of MQTT packets. In practical applications, you can directly view the packet details in Wireshark. Wireshark provides excellent support for MQTT, listing the values of each field directly without the need for our own analysis.

Of course, Wireshark does list the values of each field for us. Through the subsequent introduction of the structure of the CONNECT and CONNACK packets, combined with the packet capture results from Wireshark, you will quickly master these two packets:

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

Official download link for Wireshark:

https://www.wireshark.org/#downloadLink

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

CONNECT Packet Structure

CONNECT Packet Structure

Fixed HeaderIn the fixed header of the CONNECT message, the packet type field in the high bits of the first byte must be 1 (0b0001), while the low bits of the first byte must all be 0.Therefore, the value of the first byte of the CONNECT message must be <span>0x10</span>. We can use this to determine whether a message is a CONNECT message.Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

Variable Header Variable Header

The variable header of the CONNECT packet contains the following fields in order:Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

  • Protocol Name: This is a UTF-8 encoded string used to indicate the protocol name. In MQTT, the first two bytes of the UTF-8 encoded string are used to represent the length of the subsequent actual character data. In MQTT 3.1.1 and MQTT 5.0, the protocol name is fixed as <span>MQTT</span>, so the corresponding complete content in hexadecimal bytes is <span>00 04 4d 51 54 54</span>, where <span>4d 51 54 54</span> is the ASCII value of the string <span>MQTT</span>. The protocol name in the earliest MQTT 3.1 was <span>MQIsdp</span>, corresponding to <span>00 06 4d 51 49 73 64 70</span>.

  • Protocol Version: This is an unsigned integer used to represent the protocol version. Currently, there are only three possible values, 3 for MQTT 3.1, 4 for MQTT 3.1.1, and 5 for MQTT 5.0.

  • Connect Flags: The connect flags are only 1 byte long but contain multiple flags to indicate connection behavior or whether certain fields exist in the payload.Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK
  • User Name Flag: Indicates whether the payload contains a username.
  • Password Flag: Indicates whether the payload contains a password.
  • Will Retain: Indicates whether the last will message is retained.
  • Will QoS: Indicates the QoS of the last will message.
  • Will Flag: Indicates whether the payload contains fields related to the last will message.
  • Clean Start: Indicates whether the current connection is a new session or a continuation of an existing session, which determines whether the server creates a new session directly or attempts to reuse an existing session.
  • Reserved: This is a reserved bit, and its value must be 0.
  • Keep Alive: This is a two-byte unsigned integer used to indicate the maximum time interval between two adjacent control packets sent by the client.
  • Properties: The following table lists all available properties of the CONNECT packet:

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

Payload Payload

In the payload of the CONNECT packet, all fields except the Client ID are optional. Their presence depends on the values of the corresponding flags in the Connect Flags of the variable header. However, if these fields exist, they must appear in the order of Client ID, Last Will Properties, Last Will Topic, Last Will Payload, User Name, and Password.

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

CONNACK Packet StructureCONNACK Packet Structure

Fixed HeaderThe value of the high 4 bits of the first fixed header byte is 2 (0b0010), indicating that this is a CONNACK packet.Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

Variable Header Variable Header

The variable header of the CONNACK packet contains the following fields in order:

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

  • Connect Acknowledge Flags: Connection acknowledgment flags.

    • Reserved (Bits 7-1): Reserved bits, must be set to 0.
    • Session Present (Bit 0): Indicates whether the server is using an existing session to communicate with the client only when the client sets Clean Start to 0 in the CONNECT connection,
    • Session Present can only be 1.
  • Reason Code: Indicates the result of the connection. The following are some common Reason Codes in the CONNACK packet; for a complete list, please refer to the MQTT 5.0 Reason Code Quick Reference guide or later tweets.

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

  • Properties: The following table lists all available properties of the CONNACK packet.

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

Payload Payload The CONNACK packet has no payload.

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

Conclusion

CONNECT is the first MQTT packet sent by the client after establishing a network connection with the server. CONNACK, as the response packet to CONNECT, indicates the connection result through the reason code.

The client and server need to use the CONNECT and CONNACK messages to complete the necessary information exchange, such as protocol version, Client ID, username and password used by the client, maximum packet size supported by the server, QoS, and whether there is a corresponding session.The above is an introduction to the MQTT CONNECT and CONNACK packets. In subsequent articles, we will continue to explore the structure and composition of packets such as PUBLISH and DISCONNECT.

Reference Links:

  1. https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901004

  2. https://www.emqx.com/en/blog/mqtt-5-0-control-packets-01-connect-connack

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

  • [Video Course] Introduction to Codesys V3.5 Series(138 people have learned)【Video Course】Codesys SoftMotion Basics(39 people have learned)
  • [Video Course] Codesys SoftMotion Electronic Gears(12 people have learned)
  • [Video Course] Codesys SoftMotion Electronic Cam Courses(9 people have learned)
  • [Video Course] Making Custom Libraries in Codesys(22 people have learned)
  • Comprehensive Free Resources for Codesys V3.5 Series
  • Top 10 Common Filtering Algorithms (ST Language)

  • What Does a PLC Integrated with Chat GPT Look Like?

  • Sharing the Top 10 PLC Programming Books of 2023

  • Customize Your Own CODESYS Motion Controller

  • Can MC_Power.status = FALSE still allow the axis to move?

  • Summary of ST Language Learning Materials

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACK

——–END——–

Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACKIf you like this article, please share and “like” below and “look”

Leave a Comment