← Back to the chapter · Next detail →

Technical detail · MQTT Packet and Broker Features

Under the Hood

An MQTT PUBLISH packet carries a named message; this MQTT 3.1.1 example sends the six-byte text 83.4 C on topic plant/boiler7/temperature at quality of service (QoS) 1.

DUP marks retransmission, RETAIN requests storage of a last-known topic value, and a packet identifier matches the protocol acknowledgement exchange. Remaining Length counts bytes after the fixed header, whose minimum length is two bytes.

Read the first byte, 0x32, as PUBLISH with DUP=0, QoS=01 and RETAIN=0. Next comes 0x23: Remaining Length is 35 bytes. Those bytes comprise a two-byte topic-length field, the 25-byte plant/boiler7/temperature name, a two-byte packet identifier and the six-byte payload 83.4 C.

This separates the fixed header from the rest of the packet. The two-byte minimum is not the size of a complete reading. When QoS is greater than zero, the packet identifier belongs in the count too. Record raw bytes beside decoded fields before diagnosing a missing value.

Other packet types have other jobs. An idle client can send 0xC0 0x00, a PINGREQ (ping request) with zero remaining bytes, to show it is alive. It carries no temperature. A retained QoS 1 PUBLISH may instead start with 0x33: PUBLISH, DUP=0, QoS=01, RETAIN=1. One changed flag changes the broker’s storage behavior.

← Back to the chapter · Next detail →