PUBLISH
Fixed header flags: DUP(1) QoS(2) RETAIN(1) – all four flags active
PUBLISH carries an application message from publisher to broker, or from broker to subscriber. The fixed header flags encode DUP (retransmission flag), QoS (0, 1, or 2), and RETAIN. For QoS 1, the broker responds with PUBACK. For QoS 2, the broker responds with PUBREC starting a four-part handshake. QoS 0 has no acknowledgement.
Details
PUBLISH is the core data-carrying packet. Fixed header byte 1 encodes: Bits 7-4: 0011 (packet type 3) Bit 3: DUP – set to 1 on retransmission of an unacknowledged QoS 1/2 packet Bits 2-1: QoS level – 00=0, 01=1, 10=2 (11 is reserved and forbidden) Bit 0: RETAIN – broker stores message as retained for the topic
Variable header: Topic Name (UTF-8, length-prefixed), Packet Identifier (QoS 1/2 only, 2 bytes), Properties (MQTT 5.0).
MQTT 5.0 PUBLISH properties: Payload Format Indicator (0=binary, 1=UTF-8), Message Expiry Interval (seconds until broker discards), Topic Alias (replace full topic name with 2-byte integer), Response Topic (for request-response pattern), Correlation Data, User Properties, Subscription Identifier, Content Type.
QoS flow: QoS 0: PUBLISH → (no ACK) — fire and forget QoS 1: PUBLISH → PUBACK — at least once QoS 2: PUBLISH → PUBREC → PUBREL → PUBCOMP — exactly once
Topic Alias (MQTT 5.0): client and broker independently negotiate a mapping from a 2-byte integer to a full topic string, reducing bandwidth for repeated publishes to the same topic.
Fixed Header
| Field | Value |
|---|---|
| Packet type (bits 7–4) | 3 (0011₂) |
| First byte (hex) | 0x30 |
| Flags (bits 3–0) | DUP(1) QoS(2) RETAIN(1) – all four flags active |
| QoS bits apply | Yes – bits 2–1 encode QoS level |
| Direction | both |
| Added in | MQTT 3.1 |