What is MQTT?
MQTT is a small publish and subscribe protocol for devices. Every device keeps one TCP connection to a central broker. Sensors publish readings to topics like home/livingroom/temp, and apps subscribe to the topics they care about. The broker passes each message to whoever subscribed.
Its headers can be as small as 2 bytes, so it suits battery sensors, weak mobile links and thousands of devices at once. Smart home hubs, factory sensors, vehicle trackers and many chat and notification systems use MQTT. Features like QoS levels, retained messages and a last will handle unreliable networks.
Think of it like this. MQTT is like a notice board with labelled sections. A sensor pins a note under Living room, temperature; anyone who asked to follow that section gets a copy at once. The board can keep the latest note pinned for latecomers, and if a device stops checking in, the board posts the note it left behind: I am offline.
Step by step
Normal
5 stepsA sensor posts the living room temperature; a phone app that subscribed gets it at once.
Normal
5 stepsA sensor posts the living room temperature; a phone app that subscribed gets it at once.
- 1
Sensor connects
CONNECT and CONNACK
The sensor opens one TCP connection to the broker and keeps it open. It never talks to the phone directly; the broker sits in the middle.
- 2
App subscribes
SUBSCRIBE home/livingroom/temp
The phone app connects too and subscribes to a topic. The broker remembers who wants what.
- 3
QoS 0 publish
Fire and forget
The sensor publishes a reading with QoS 0. The broker passes it on straight away. Nobody confirms it, which is fine for a value that repeats every minute.
- 4
QoS 1 publish
PUBLISH and PUBACK
For a reading that must arrive, QoS 1 asks each receiver to confirm with PUBACK. The sensor to broker hop and the broker to phone hop are confirmed separately.
- 5
Keep alive
PINGREQ and PINGRESP
When it has nothing to say, the sensor sends a 2 byte ping so the broker knows it is still there.
Retained message
3 stepsThe app opens long after the last reading, yet shows the temperature at once.
Retained message
3 stepsThe app opens long after the last reading, yet shows the temperature at once.
- 1
Publish with retain
RETAIN flag set
The sensor publishes with the retain flag. The broker passes it on as usual and also keeps it as the last known value for that topic.
- 2
App opens later
Connect and subscribe
Omar opens the app ten minutes later. Without retain he would see nothing until the next reading.
- 3
Instant value
Broker sends the kept copy
Right after the SUBACK, the broker sends the retained message, marked as retained, so the app shows 21.6 straight away.
Last will
4 stepsThe sensor's battery dies. The broker notices the silence and publishes its last will.
Last will
4 stepsThe sensor's battery dies. The broker notices the silence and publishes its last will.
- 1
Connect with a will
If I vanish, say offline
In CONNECT the sensor gives the broker a will message: topic home/livingroom/sensor/status, payload offline, retained.
- 2
Silence
No more packets
The battery runs out. The sensor cannot send DISCONNECT, so the TCP connection just goes quiet.
- 3
Timeout
One and a half keep alives
With a 60 second keep alive, the broker waits 90 seconds without hearing anything, then closes the connection.
- 4
Will published
Everyone learns it is offline
The broker publishes the will to subscribers. The app shows Sensor offline instead of an old, misleading temperature.
TLS on 8883
3 stepsA sensor talks to a broker over the internet, so it uses TLS on port 8883 and a password.
TLS on 8883
3 stepsA sensor talks to a broker over the internet, so it uses TLS on port 8883 and a password.
- 1
TLS handshake
Port 8883
The sensor connects to port 8883 and runs a TLS handshake. It checks the broker's certificate so it cannot be fooled by a fake broker.
- 2
Login inside TLS
Username and password, encrypted
MQTT's username and password travel in plain text inside CONNECT. On 1883 anyone on the path could read them; on 8883 they are encrypted.
- 3
Publish
Same MQTT, hidden
Everything after that is ordinary MQTT, just encrypted. An onlooker sees only the server name and the size and timing of records.
Myths and mix-ups
- MQTT devices do not talk to each other directly; every message goes through the broker.
- MQTT is not encrypted by default; port 1883 sends passwords and data as plain bytes.
- QoS 2 is not always better; it costs four packets per message and most sensors only need QoS 0 or 1.
- The name MQTT is no longer an acronym; the standard just calls it MQTT.
Try it yourself
Run these on your own computer to see MQTT at work.
Run a broker on your machine
mosquitto -vStarts the open source Mosquitto broker on port 1883 and prints every connect and publish.
Subscribe
mosquitto_sub -h localhost -t 'home/#' -v-v prints the topic next to each message. # receives everything under home/.
Publish
mosquitto_pub -h localhost -t home/livingroom/temp -m 21.5 -q 1 -r-q 1 asks for QoS 1 and -r retains it. Run mosquitto_sub again and the value appears at once.
Watch the packets
sudo tcpdump -n -i lo0 -X tcp port 1883On Linux use -i lo. You can read the topic and payload in the hex dump, which shows why TLS matters.
MQTT at a glance
- Layer
- Application (7)
- Default port
- 1883, or 8883 for MQTT over TLS
- Transport
- TCP, also WebSocket
- Smallest packet
- 2 bytes (PINGREQ)
- Delivery levels
- QoS 0, 1 and 2
- Pattern
- Publish and subscribe through a broker
- Versions
- 3.1.1 (OASIS 2014, ISO/IEC 20922) and 5.0 (OASIS 2019)
Defined inOASIS MQTT Version 5.0OASIS MQTT Version 3.1.1ISO/IEC 20922:2016
Header fields
What MQTT puts in its messages, with example values from the stories above.
MQTT 5.0
- Packet type
1 (CONNECT)- Fixed header
0x10The first byte holds the packet type and, for PUBLISH, the DUP, QoS and RETAIN flags.- Protocol name and level
MQTT, 5Level 5 is MQTT 5.0. Level 4 would be MQTT 3.1.1.- Connect flags
Clean start- Keep alive
60 secondsIf the broker hears nothing for one and a half times this, it treats the client as gone.- Client ID
livingroom-tempEach client needs a unique ID. A second client with the same ID kicks the first one off.- Session present
0- Reason code
0x00 (Success)Codes of 0x80 and above are refusals, such as 0x86 bad user name or password.- Packet ID
1The SUBACK will carry the same ID.- Topic filter
home/livingroom/tempTopics are paths split by slashes. + matches one level and # matches everything below.
Test yourself
In MQTT, who delivers a published message to the subscribers?
Questions people ask
What port does MQTT use?
1883 for plain MQTT and 8883 for MQTT over TLS. Brokers that accept MQTT over WebSocket often use another port such as 8083 or 443.
What is an MQTT broker?
The server in the middle. Every device connects to it, and it receives each published message and forwards it to all clients subscribed to that topic.
What is the difference between MQTT QoS 0, 1 and 2?
QoS 0 sends once with no confirmation. QoS 1 repeats until it gets a PUBACK, so it may arrive twice. QoS 2 uses a four step exchange so it arrives exactly once.
Is MQTT secure?
Only if you set it up that way. Use TLS on port 8883, require a username and password or client certificates, and limit which topics each client may use.
What is the difference between MQTT and HTTP?
HTTP is request and response: a client asks and waits. MQTT keeps one connection open and pushes messages to subscribers the moment they are published, with much smaller headers.