
In the previous issue, we introduced the CONNECT and CONNACK packets of MQTT 5.0:Detailed Explanation of MQTT Packets【01】: CONNECT & CONNACKNow, we will introduce the PUBLISH packet used for transmitting application messages in MQTT and its response packet.
Whether the client publishes messages to the server or the server forwards messages to subscribers, the PUBLISH packet is required. The topic that determines the message direction, the actual content of the message, and the QoS level are all included in the PUBLISH packet.
During the message transmission process between the client and server, in addition to the PUBLISH packet, there are four other packets: PUBACK, PUBREC, PUBREL, and PUBCOMP.

Sample Example
We use MQTTX CLI to publish three messages with different QoS levels to the public MQTT server, and use Wireshark to capture the MQTT packets transmitted back and forth between the client and server. In Linux, you can use tcpdump to capture packets and then import them into Wireshark for viewing.
The following is the MQTTX CLI command used in this example. To display the properties of the PUBLISH packet, these commands also set the Message Expiry Interval and Response Topic properties:
mqttx pub --hostname broker.emqx.io --mqtt-version 5 \ --topic request --qos 0 --message "This is a QoS 0 message" \ --message-expiry-interval 300 --response-topic response
The following is the PUBLISH packet captured by Wireshark with QoS 0:
This hexadecimal byte string corresponds to the following packet content:
When we only modify the QoS option in the MQTTX CLI command and set it to 1, we will see the server reply with a PUBACK packet after receiving the PUBLISH. Their packets are as follows.
At this point, the first byte in the PUBLISH packet has changed from<span><span>0x30</span></span> to<span><span>0x32</span></span>, indicating that this is a QoS 1 message.The PUBACK packet structure is relatively simple, as you can see, the Reason Code is 0x10, indicating that the message has been received, but there are no matching subscribers. Once someone subscribes to the topic<span><span>request</span></span>, the Reason Code in the PUBACK packet will change to<span><span>0x00</span></span>, indicating that the message has been received and there are matching subscribers.
Continuing to publish QoS 2 messages using MQTTX CLI, we will see two rounds of message exchange between the client and server. Wireshark will tell us that these packets are PUBLISH, PUBREC, PUBREL, and PUBCOMP, all with the same packet identifier<span><span>0x11c2</span></span>:

How do we accurately determine from the packet data composed of hexadecimal bytes whether it is a PUBLISH packet, what its QoS level is, and what the Reason Code is in its response packet? The following introduction to these packets will answer these questions.

PUBLISH Packet Structure
PUBLISH Packet Structure
Fixed HeaderIn the Fixed Header of the PUBLISH packet, the high 4 bits of the first byte are fixed to 3 (0 b 0011), and the low 4 bits consist of the following three fields:
- DUP (Bit 3): When the client or server retransmits the PUBLISH packet, the DUP flag needs to be set to 1, indicating that this is a retransmitted packet.When DUP is set to 1, the number and frequency of received PUBLISH packets can reveal the quality of the current communication link.
- QoS (Bit 2 – 1): Used to specify the QoS level of the message.
- Retain (Bit 0): Set to 1, indicating that the current message is a retained message; set to 0, indicating that the current message is a normal message.
Following this is the Remaining Length field, which indicates the number of bytes in the remaining part of the current packet.
Variable Header Variable Header
The variable header of the PUBLISH packet sequentially contains the following fields:
- Topic Name: This is a UTF-8 encoded string used to indicate which channel the message should be published to.
- Packet Identifier: This is a two-byte identifier used to uniquely identify the message currently being transmitted. When the QoS level is 1 or 2, the packet identifier only appears in the PUBLISH packet.
- Properties: The following table lists all available properties of the PUBLISH packet.

Payload Payload
The content of the application message we send is stored in the Payload of the PUBLISH packet. It can carry application messages in any format, such as JSON, ProtoBuf, etc.
PUBACK Packet StructurePUBACK Packet Structure
Fixed HeaderThe high 4 bits of the first byte in the fixed header are fixed to 4 (0b0100), indicating that this is a PUBACK packet. The lower 4 bits are reserved and all set to 0.
Following this is the Remaining Length field, which indicates the number of bytes remaining in the current packet.
Variable Header Variable Header
The variable header of the PUBACK packet sequentially contains the following fields:
- Packet Identifier: Unlike the PUBLISH packet, the packet identifier in the PUBACK packet must exist. It is used to indicate to the other end which QoS 1 PUBLISH packet this is a response to.
Reason Code:Reason Code: This is a single-byte string used to indicate the result of the publication to the publisher, such as whether the publication was rejected due to lack of authorization. The following table lists all available reason codes for the PUBACK packet:

- Properties: The following table lists all available properties of the PUBACK packet.

Payload Payload
The PUBACK packet has no payload.

PUBREC, PUBREL, PUBCOMP Packet Structure
The structures of PUBREC, PUBREL, and PUBCOMP packets are basically the same as PUBACK. Their main difference lies in the value of the packet type field in the fixed header and the available reason codes.The value of the packet type field is 5, indicating that this is a PUBREC packet; if the value is 6, it indicates that this is a PUBREL packet; if the value is 7, it indicates that this is a PUBCOMP packet.PUBREC, as the acknowledgment message of the PUBLISH message in the QoS 2 message flow, can use the same reason codes as PUBACK. As shown in the figure below:
The available reason codes for PUBREL and PUBCOMP packets are as follows:

Conclusion
The Topic in the PUBLISH packet determines the direction of the message, while the QoS determines the reliability of the message. It also determines which packets will be used during transmission. The PUBACK packet is used for QoS 1 messages, while PUBREC, PUBREL, and PUBCOMP packets are used for QoS 2 messages. When QoS is greater than 0, the packet must also include a packet identifier to associate the PUBLISH packet with its response packet.
The Payload of the PUBLISH packet is not restricted to data types, so we can transmit application messages in any format. Additionally, the Properties can meet the needs of more scenarios. For example, Topic Expiry can reduce the size of each message, and Message Expiry Interval can set expiration times for time-sensitive messages, etc.The response packets of the PUBLISH packet not only indicate to the sender that the message has been received but also further indicate the result of the publication through the Reason Code. Therefore, when subscribers cannot receive messages, we can also troubleshoot issues through the reason codes in the response packets received by the publisher.
Reference Links:
-
http://www.steves-internet-guide.com/mqtt-publish-subscribe/
-
https://www.emqx.com/en/blog/mqtt-5-0-control-packets-02-publish-puback

- [Video Course] Introduction to Codesys V3.5 Series(138 people have learned)
- [Video Course] Basics of Codesys SoftMotion Control(39 people have learned)
- [Video Course] Codesys SoftMotion Electronic Gear Course(12 people have learned)
- [Video Course] Codesys SoftMotion Electronic Cam Course(9 people have learned)
- [Video Course] Creating Custom Libraries in Codesys(22 people have learned)
-
Comprehensive Free Resources for Codesys V3.5 Series
-
Top Ten 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
-
MC_Power.status = FALSE, Can the Axis Still Move?
-
Summary of ST Language Learning Materials

——–END——–
If you like this article, please share and “like” it below and “view” it