Skip to main content
8

SUBSCRIBE

0x82Client to Server

Fixed header flags: 0010 – bit 1 must be 1 (spec §3.8.1)

SUBSCRIBE registers one or more topic filters with the broker. Each filter has a requested QoS and, in MQTT 5.0, options: No Local, Retain As Published, and Retain Handling. The broker responds with SUBACK containing one reason code per requested filter. A single SUBSCRIBE may contain up to 65535 topic filter entries.

Details

The SUBSCRIBE packet registers client interest in one or more topics. Fixed header byte is 0x82 (flags 0010 are mandatory per §3.8.1).

Each topic filter entry in the payload contains: Topic Filter (UTF-8): e.g., "home/+/temp", "sensors/#" Subscription Options byte: Bits 1-0: Maximum QoS (the maximum QoS at which the broker will deliver messages) Bit 2: No Local (MQTT 5.0) – suppress delivery of messages published by this client Bit 3: Retain As Published (MQTT 5.0) – preserve the RETAIN flag from the original PUBLISH Bits 5-4: Retain Handling (MQTT 5.0) – 0=send retained on subscribe, 1=only if no existing subscription, 2=never Bits 7-6: reserved, must be 0

Topic filter wildcards: + (plus): matches exactly one topic level. "home/+/temp" matches "home/kitchen/temp", "home/bedroom/temp" but not "home/temp" # (hash): matches zero or more levels, must be the last character. "home/#" matches "home/", "home/kitchen", "home/kitchen/temp" $SYS: broker system topics, not matched by # alone – must explicitly subscribe to $SYS/#

Shared subscriptions (MQTT 5.0): "$share/{GroupName}/{TopicFilter}" – messages are load-balanced across all clients in the group, not delivered to each.

Fixed Header

FieldValue
Packet type (bits 7–4)8 (1000₂)
First byte (hex)0x82
Flags (bits 3–0)0010 – bit 1 must be 1 (spec §3.8.1)
QoS bits applyNo
Directionclient-to-server
Added inMQTT 3.1

See Also