MQTT cheat sheet

The numbers and names you look up while working with MQTT, on one page.

QoS levels

QoS Guarantee Packets Duplicates
0At most once. One send, no acknowledgement, no retry.PUBLISHNo, but the message can be lost
1At least once. Resent until acknowledged.PUBLISH, PUBACKPossible
2Exactly once. Two-step handshake.PUBLISH, PUBREC, PUBREL, PUBCOMPNo

QoS 0 suits frequent telemetry where a lost reading does not matter. QoS 1 suits most state and command traffic. QoS 2 is for commands that must not run twice. The MQTT QoS levels page goes into how each level behaves across a reconnect.

Control packet types

MQTT 5 defines 15 control packet types, values 1 to 15. MQTT 3.1.1 has 14, without AUTH. Value 0 is reserved in both.

Value Name Direction Purpose
1CONNECTClient to brokerConnection request
2CONNACKBroker to clientConnection acknowledgement
3PUBLISHEitherPublish a message
4PUBACKEitherQoS 1 acknowledgement
5PUBRECEitherQoS 2 step 1
6PUBRELEitherQoS 2 step 2
7PUBCOMPEitherQoS 2 step 3
8SUBSCRIBEClient to brokerSubscribe request
9SUBACKBroker to clientSubscribe acknowledgement
10UNSUBSCRIBEClient to brokerUnsubscribe request
11UNSUBACKBroker to clientUnsubscribe acknowledgement
12PINGREQClient to brokerKeep alive ping
13PINGRESPBroker to clientKeep alive reply
14DISCONNECTEither in MQTT 5, client only in 3.1.1Graceful disconnect
15AUTHEither, MQTT 5 onlyExtended authentication

Wildcards

+ matches one topic level. # matches the rest of the topic and must come last, so sport/# also matches sport. A pattern that starts with + or # does not match a topic starting with $, and neither is allowed in a topic you publish to. Try patterns in the wildcard tester.

Default ports

Port Use
1883MQTT over TCP, unencrypted. IANA registered.
8883MQTT over TLS. IANA registered.
8080, 8081MQTT over WebSocket, ws and wss. Broker convention.
9001Mosquitto's usual WebSocket listener. Broker convention.

Only 1883 and 8883 are standard. Check the broker's listener configuration for the rest. More on the MQTT ports page.

Retained messages

A message published with the retain flag is stored by the broker, one per topic, and sent to every new subscriber at once. A new retained message replaces it. An empty retained payload deletes it. See how retained messages work and how to clear them.

Last Will and Testament

A message the client registers at CONNECT time with its own topic, payload, QoS and retain flag. The broker publishes it if the client drops without sending DISCONNECT. In MQTT 5 a Will Delay Interval holds it back, so a quick reconnect does not announce a false offline state.

Clean session, clean start and session expiry

MQTT 3.1.1 has one Clean Session flag. True discards subscriptions and queued QoS 1 and 2 messages on disconnect. False keeps them until the client returns.

MQTT 5 splits it in two. Clean Start says whether this connection resumes the old session. Session Expiry Interval says how many seconds the broker keeps the session after a disconnect. Zero means discard at once.

Keep alive

An interval in seconds agreed at CONNECT time. If nothing else has been sent within it, the client sends PINGREQ and the broker answers PINGRESP. If the broker hears nothing for one and a half times the interval, it treats the client as gone and publishes its Last Will.

MQTT 5 additions

Feature What it does
User propertiesKey and value string pairs on any packet, for application metadata.
Topic aliasA small integer that stands in for a topic string after the first publish.
Response topic and correlation dataRequest and response over PUBLISH: where to reply, and how to match the reply.
Shared subscriptions$share/group/topic splits messages between subscribers in the group.
Message expiry intervalSeconds after which the broker drops an undelivered message.
Reason codesEvery acknowledgement carries a specific code instead of pass or fail.
Receive maximumHow many QoS 1 and 2 messages each side allows in flight.

Reason codes

Below 0x80 is success, 0x80 and above is failure. The same numbering is used, with packet-specific subsets, in CONNACK, PUBACK, SUBACK, UNSUBACK and DISCONNECT.

Hex Name In practice
0x00SuccessAccepted
0x80Unspecified errorCheck the broker's logs
0x81Malformed packetThe packet could not be parsed
0x82Protocol errorThe packet broke the specification, for example wrong flags
0x84Unsupported protocol versionThe broker does not speak the requested MQTT version
0x85Client identifier not validEmpty when not allowed, bad characters, or too long
0x86Bad user name or passwordCredentials wrong or missing
0x87Not authorisedCredentials fine, this client is not allowed
0x88Server unavailableThe broker cannot accept connections now
0x89Server busyBack off and retry
0x8ABannedBlocked, often after repeated failed authentication
0x97Quota exceededA broker limit was hit, for example too many connections
0x99Payload format invalidThe payload claims UTF-8 and is not
0x9CUse another serverThe broker wants this client elsewhere

$SYS topics

Brokers publish their own metrics under $SYS/: connected clients, message rates, uptime. It is a convention, not part of the specification, and every broker lays it out differently. A subscription to # does not include it; subscribe to $SYS/#. What each broker publishes is in the $SYS topics reference.

Frequently asked questions

What is the default MQTT port?

1883 for plain MQTT over TCP and 8883 for MQTT over TLS. Both are registered with IANA. WebSocket ports are broker convention, commonly 8080, 8081 or 9001.

What is the difference between QoS 1 and QoS 2?

QoS 1 resends until the receiver acknowledges, so a message can arrive twice. QoS 2 adds a second handshake so it arrives exactly once, at the cost of two more packets per message.

What does MQTT reason code 0x87 mean?

Not authorised. The broker accepted the credentials as valid but this client is not allowed to connect, publish or subscribe. Compare 0x86, bad user name or password, where the credentials themselves are wrong.

What is MQTT keep alive?

An interval in seconds agreed at connect time. If the client sends nothing else within it, it sends a PINGREQ. If the broker hears nothing for one and a half times the interval, it drops the client and publishes its Last Will.

MQTT Viewer shows QoS and retain on every message, publishes with MQTT 5 properties and turns $SYS topics into a status page. Download MQTT Viewer.

MQTT Viewer is free and open source, and runs on macOS, Windows and Linux.

Download MQTT Viewer