Cheat sheet

MQTT cheat sheet

Topic rules, QoS flows, retained messages, Will messages, sessions and the MQTT 5 additions, each tied to its section in the OASIS MQTT 3.1.1 and 5.0 standards.

Last verified 28 November 2026· MQTT 3.1.1 (OASIS Standard plus Errata 01), MQTT 5.0 (OASIS Standard)

This page collects the MQTT (originally MQ Telemetry Transport) rules that matter when you write device firmware or design a topic tree. It covers both versions in use today: MQTT 3.1.1 and MQTT 5.0. Each row cites the section of the OASIS standard it comes from, so you can check the exact wording before you rely on it. Where 5.0 changes the behaviour, both are given.

Versions

MQTT 3.1.1 MQTT 5.0
Status OASIS Standard; the current text incorporates Errata 01 (10 December 2015) OASIS Standard, approved 7 March 2019
Protocol level byte in CONNECT 4 (section 3.1.2.2) 5 (section 3.1.2.2)
Session control Clean Session flag Clean Start flag plus Session Expiry Interval
Result codes Return codes in CONNACK and SUBACK only A Reason Code on every acknowledgement, plus DISCONNECT and AUTH

Both texts note that IANA has registered TCP port 1883 for MQTT and 8883 for MQTT over TLS (Transport Layer Security). That note is non-normative, so a broker may listen elsewhere.

Topic names and filters

A topic name is what a publisher sends to. A topic filter is what a subscriber asks for, and only filters may contain wildcards (MQTT 5.0 section 4.7).

Rule Detail
Level separator / splits levels. a//b has an empty middle level.
# multi-level wildcard Must be last, alone or after /. sport/# also matches sport. # alone matches everything except $ topics.
+ single-level wildcard Fills exactly one whole level. sport/+ matches sport/ but not sport. sport+ is invalid.
Case Case sensitive: ACCOUNTS and Accounts are different topics.
Leading slash /finance and finance are different topics. /finance matches +/+ and /+ but not +.
Encoding UTF-8, at least one character, at most 65,535 bytes, no null character. Spaces are allowed.
Matching The broker compares level by level, character for character, with no normalisation.
$ topics Filters starting with # or + never match names starting with $. $SYS/ is the common prefix for broker information. To get both, subscribe to # and $SYS/#. Applications should not publish their own $ topics.

The standard puts no limit on the number of levels beyond the 65,535-byte string length. A pattern many fleets use is site/device-id/kind/name, with the device identifier in a fixed position so that one + selects a whole device class. That is a design choice, not part of the standard.

Quality of Service

QoS Promise (section 4.3) Packets Packet Identifier
0 At most once. No reply and no retry. PUBLISH None
1 At least once. Duplicates are possible. PUBLISH, PUBACK Yes, freed when PUBACK arrives
2 Exactly once, at the cost of a second round trip PUBLISH, PUBREC, PUBREL, PUBCOMP Yes, freed when PUBCOMP arrives

QoS applies to one hop. A message published to the broker at QoS 2 can reach a subscriber at a lower QoS, because each delivery is negotiated separately and the subscriber sets a maximum QoS when it subscribes (sections 4.3 and 3.8.3.1).

QoS 1: at least onceSenderReceiverPUBLISH id=7PUBACK id=7id 7 free for reuseQoS 2: exactly onceSenderReceiverPUBLISH id=8PUBREC id=8PUBREL id=8PUBCOMP id=8id 8 free for reuse
QoS 1 and QoS 2 delivery between one sender and one receiver, drawn from the non-normative flow figures in MQTT 5.0 section 4.3. The same packet identifier (here 7 and 8) ties each acknowledgement to its PUBLISH.

Retained messages

Rule 3.1.1 5.0
Storing PUBLISH with RETAIN=1 replaces the stored message for that topic (3.3.1.3) Same (3.3.1.3)
Clearing RETAIN=1 with a zero-byte payload deletes the retained message Same, and a zero-byte retained message is never stored
QoS 0 retained The broker should store it but may discard it at any time Same
Sent on subscribe Always, with RETAIN=1 Controlled by Retain Handling: 0 always, 1 only for a new subscription, 2 never (3.8.3.1)
RETAIN flag on live forwards Always 0 0, unless the subscriber set Retain As Published
Expiry None A retained message with a Message Expiry Interval is discarded when it expires

Retained messages suit state that changes rarely, such as a device’s configuration or its online status, because a new subscriber gets the latest value at once.

Will message (last will)

The client registers a Will message in CONNECT. The broker publishes it when the network connection closes without a clean disconnect, for example after an I/O error, a missed keep-alive, or the client closing the connection without sending DISCONNECT (3.1.2.5 in both versions).

  • In 3.1.1 any DISCONNECT deletes the Will. In 5.0 only DISCONNECT with Reason Code 0x00 (Normal disconnection) deletes it.
  • MQTT 5.0 adds the Will Delay Interval (3.1.3.2.2, default 0). The broker waits that many seconds, or until the session ends if that is sooner. If the client reconnects to the same session in time, the Will is not sent. This hides short Wi-Fi dropouts from the rest of the system.
  • Both versions let the Will itself be retained (Will Retain flag, 3.1.2.7). One pattern built on this: a retained Will of offline on a status topic, and a retained online that the client publishes after it connects.

Sessions and keep alive

Setting 3.1.1 5.0
Start fresh CleanSession=1: the session lasts only as long as the connection (3.1.2.4) Clean Start=1 (3.1.2.4)
Keep state while offline CleanSession=0: the broker stores QoS 1 and QoS 2 messages for the client’s subscriptions Clean Start=0 with a Session Expiry Interval above 0 (3.1.2.11.2)
How long state is kept Until a later connection with CleanSession=1 Session Expiry Interval in seconds. 0 or absent ends the session with the connection. 0xFFFFFFFF never expires.
Equivalents Clean Start=1 with expiry 0 equals CleanSession=1. Clean Start=0 with no expiry property equals CleanSession=0.

Keep Alive (3.1.2.10) is a 16-bit value in seconds. If the client has nothing else to send within that time, it sends PINGREQ. If the broker receives nothing for 1.5 × the Keep Alive period, it closes the connection, which triggers the Will. A value of 0 turns the mechanism off. In 5.0 the broker can return a Server Keep Alive in CONNACK, and the client must use it.

For a battery device that sleeps between reports, the keep-alive only matters while it is connected. If it disconnects before sleeping, send a normal DISCONNECT so the broker does not publish the Will.

MQTT 5.0 additions

Appendix C of the 5.0 standard lists the new features. The ones most useful on devices:

Feature Section What it gives you
Reason Codes 2.4 Every acknowledgement says what happened. Below 0x80 is success, 0x80 and above is failure.
Server DISCONNECT 3.14 The broker can say why it is closing the connection.
User Properties 3.3.2.3.7 Name and value string pairs on most packets. Those on PUBLISH travel with the message.
Message Expiry Interval 3.3.2.3.3 Lifetime in seconds. The broker drops the copy for a subscriber if delivery has not started in time, and forwards the remaining lifetime.
Topic Alias 3.3.2.3.4 Replace a long topic with a small integer for the rest of one connection. 0 is not allowed. Each side sets its own Topic Alias Maximum.
Payload Format and Content Type 3.3.2.3.2, 3.3.2.3.9 Mark the payload as UTF-8 text or binary, and give a MIME-style type.
Request and response 4.10 Response Topic and Correlation Data let a reply find its way back to the requester.
Shared Subscriptions 4.8.2 $share/{ShareName}/{filter}: each message goes to only one session in the group. Useful for load-sharing back-end workers.
Subscription Identifier 3.8.2.1.2 A number from 1 to 268,435,455 set on SUBSCRIBE and returned on each matching message.
Subscription options 3.8.3.1 No Local (do not echo my own messages), Retain As Published and Retain Handling.
Flow control 4.9 Receive Maximum caps unacknowledged QoS 1 and 2 messages in flight. Default 65,535.
Maximum Packet Size 3.1.2.11.4 A small device can tell the broker the largest packet it will accept. The broker discards larger ones for that client instead of sending them.
Enhanced authentication 4.12 Challenge and response authentication through AUTH packets, including re-authentication on a live connection.
Server Reference 4.11 The broker can point the client at another server.

Size limits

Item Limit Section (5.0)
Remaining Length (one packet, after the fixed header) 268,435,455 bytes, encoded in at most four bytes 1.5.5, 2.1.4
UTF-8 string, including topic names 65,535 bytes 1.5.4, 4.7.3
Topic Alias, Receive Maximum 1 to 65,535 (two-byte integers) 3.3.2.3.4, 3.1.2.11.3

Brokers and client libraries can impose lower limits than the protocol allows, which is why 5.0 added Maximum Packet Size. Check the broker’s configuration and your library’s buffer size before you send large payloads from a microcontroller.