What is MQTT?
How MQTT works: brokers, topics, QoS levels, retained messages, last will, sessions and MQTT 5 features, with guidance on topic design and when to use it.
AI-generated image: Internet of Things News / Google Gemini
After reading this article you will be able to:
- Understand who receives the messages a device sends, and who passes them on
- Choose, for each kind of message, between speed and a confirmation of delivery
- Know when MQTT is the right tool and when another system works better
Drafted with AI from primary sources · checked by a separate AI agent · spot-checked by our editors · How we check
On this page
MQTT is an application protocol widely used by IoT devices to send data to a back end, the servers and software that collect the data and run the service, and to receive commands from it. It was designed in 1999 by engineers at IBM and Arcom for monitoring oil pipelines over satellite links, so it had to be small, tolerate links that drop without warning, and use as few bytes as possible. Those constraints describe many IoT deployments, and after it became an open standard it became a common language between devices and IoT platforms.
The protocol is simple to learn, but using it well involves decisions about how messages are named, how sure their delivery must be and what happens when a device goes offline, and those decisions are easy to get wrong. The first part of this guide explains the basics in plain terms. The second part, for readers who want to go deeper, covers connections, sessions, MQTT 5, security and a sample exchange.
How does MQTT work?
MQTT is a publish-subscribe protocol: whoever has something to say publishes it, and whoever wants it subscribes, that is, asks to receive it. Every device or application that connects is called a client. Clients do not talk to each other; they all connect to a server that sorts and passes on the messages, the broker. A client publishes a message to a topic, a name that says what the message is about, and the broker delivers it to every client that has subscribed to that topic. The sender, called the publisher, does not know or care who is listening; the receivers are called subscribers.
This separation between senders and receivers, which engineers call decoupling, is the point. A sensor publishes temperature readings to one topic; a dashboard, an alarm service and a database writer can all subscribe without the sensor’s firmware changing.
+ and # wildcards let one subscription cover many topics.What are MQTT topics?
A topic is a text string (in UTF-8 encoding) organized in levels separated by slashes, such as:
factory/line-3/press-7/temperature
Subscribers can use wildcards. The single-level wildcard + matches one level (factory/line-3/+/temperature matches every machine on line 3), and the multi-level wildcard # matches everything below (factory/line-3/#). Topics are not declared in advance; they exist when someone publishes or subscribes.
Topic design is the most consequential decision in an MQTT system. A hierarchy that runs from general to specific makes wildcard subscriptions useful and makes it easier to control which client may read or write which topics (access control). Common conventions separate the readings devices send (telemetry) from the commands they receive, and include the device identity in the topic so that the broker can enforce that each device only publishes to its own branch. Sparkplug is a specification that standardizes topic layout and the format of the message content, the payload, for industrial use.
What are the QoS levels?
Every message a client publishes carries a Quality of Service level, QoS for short: a setting that says how sure its delivery must be between the client and the broker. Choosing it means choosing between speed and a confirmation that the message arrived.
QoS 0, at most once. The message is sent once with no acknowledgment. If the connection drops at the wrong moment it is lost. Cheapest, and fine for frequent readings where the next one will arrive soon anyway.
QoS 1, at least once. The sender keeps the message until the receiver acknowledges it. If the connection drops first, the sender resends it when the client reconnects and resumes the same session; MQTT 5 forbids resending at any other time, so there is no retry timer on a live connection. The message can therefore arrive more than once, so consumers must tolerate duplicates. This is the common default for telemetry and commands.
QoS 2, exactly once. An exchange of four messages between sender and receiver (a handshake) ensures single delivery. It costs more round trips, messages going back and forth, and more state, information both sides must keep, and is reserved for things like billing events where a duplicate is as bad as a loss.
The level applies to each leg of the journey (each hop): from the publisher to the broker, and from the broker to each subscriber. The broker delivers to each subscriber at the lower of the publisher’s and the subscriber’s level.
When should you use MQTT?
MQTT suits devices that are connected to an IP network (the kind of network the internet and most home and office networks use), can keep a TCP session open at least intermittently (TCP is the internet’s standard way of keeping a reliable link open between two machines), and send many small messages to a back end that passes them on to several applications. That describes most Wi-Fi, Ethernet and cellular IoT devices, and most gateways, the boxes that link local devices to the internet.
It is a poor fit when the device cannot sustain TCP and TLS, the encryption that protects the link, which points to CoAP over DTLS or MQTT-SN, a lighter variant described below. It is also the wrong tool for request-response interactions where a client wants an immediate answer from a specific server; MQTT 5 adds response topics and correlation data for the purpose, but an ordinary web (REST) call or a CoAP call is more natural. For very high volumes of messages between back-end services, a dedicated message queue or streaming platform is a better choice than the device-facing broker. And for industrial real-time control, the wired networks that link factory machines (fieldbus protocols) and OPC UA remain the standards, with MQTT used to carry data upward from them.
For readers who want to go deeper
The rest of this guide is for readers who will set up or program MQTT devices. It uses the specification’s exact terms and covers what the connection runs on, sessions, MQTT 5, security and a sample exchange.
What does an MQTT connection run on?
A connection is a long-lived TCP session, normally wrapped in TLS, on port 1883 for plain MQTT and 8883 for TLS, the two ports registered with IANA for the protocol. MQTT can also run over WebSocket so that browser applications can subscribe directly. For devices that cannot afford TCP at all, MQTT-SN is a variant for UDP and non-IP networks that a gateway bridges to a normal broker.
How do sessions, keep-alive and last will work?
When a client connects it tells the broker whether to keep session state. A persistent session stores the client’s subscriptions and any QoS 1 or 2 messages that arrive while it is offline, delivering them on reconnect. This is how a device that sleeps for an hour receives the commands sent while it was away. The cost is broker memory, so platforms limit how much they queue and for how long.
Clients send a periodic keep-alive so that the broker can detect a dead connection. If a client disappears without a proper disconnect, the broker publishes its Last Will and Testament, a message registered at connect time, typically to a status topic.
A retained message is the last message on a topic that the broker stores and delivers immediately to any new subscriber. Publishing device status as retained means a dashboard that starts up sees the last reported state of every device at once. That is the last state published, not a guarantee that it is still true: include a timestamp in the payload, and in MQTT 5 a message expiry where stale values would mislead.
What did MQTT 5 add?
MQTT 3.1.1, standardized by OASIS in October 2014, is still the most widely deployed version. MQTT 5, published as an OASIS Standard in March 2019, addressed gaps found in large deployments:
- Reason codes on acknowledgments, so a client learns why a publish or subscribe was refused instead of being silently disconnected.
- User properties, arbitrary key-value metadata on messages, useful for content type, tracing identifiers or routing hints.
- Message expiry, so that a stale command is discarded rather than delivered to a device that reconnects a day later.
- Shared subscriptions, where a group of consumers split the messages on a topic between them for load balancing.
- Topic aliases, and the old clean-session flag split in two: Clean Start says whether to discard any existing session on connect, and the Session Expiry Interval says how long the broker keeps the session after the client disconnects. To resume a session, a client connects with Clean Start set to 0 and a non-zero expiry.
Most brokers and platforms support both versions. New designs should use 5 unless a device library does not.
How is MQTT secured?
MQTT does carry its own authentication fields: the CONNECT packet has a username and a password field in both 3.1.1 and 5.0, and MQTT 5 adds enhanced authentication through the AUTH packet, which lets client and broker negotiate challenge-response mechanisms in the style of SASL. What MQTT has no provision for is encryption: confidentiality is delegated to TLS, on port 8883 by convention, and a password sent without TLS travels in clear text. In practice the common deployments are mutual TLS with the device presenting an X.509 certificate, or a token-based scheme carried over TLS where certificates are impractical. Authorization is enforced by the broker through access control lists that bind each client identity to the topics it may use. A broker that lets any device subscribe to # has made every device a listener on every other. The device identity guide covers the certificate side.
What does a minimal exchange look like?
A device reports a reading with QoS 1 and marks its presence with a retained status message and a will:
CONNECT clientId=sensor-0042, will: devices/sensor-0042/status = "offline" (will retain = 1)
PUBLISH devices/sensor-0042/status "online" (retained)
PUBLISH devices/sensor-0042/telemetry {"t":21.4,"h":48} QoS 1
SUBSCRIBE devices/sensor-0042/cmd/# QoS 1
The back end subscribes to devices/+/telemetry and devices/+/status, and publishes commands to devices/sensor-0042/cmd/set-interval. The will is marked retained on purpose: the “online” status is retained, so a will published without the Will Retain flag would reach only the clients connected at that moment, and a dashboard started later would still be handed the old “online”.
Not every broker implements the whole specification. AWS IoT Core, for example, supports QoS 0 and 1 only and does not support the MQTT 5 AUTH packet, so check the broker’s list of supported features before designing around QoS 2 or enhanced authentication.
Where to go next
- CoAP explained covers the main alternative for constrained devices.
- IoT platforms explained describes the managed brokers most deployments use.
- IoT connectivity overview explains the radio layer underneath.
- How IoT works: from sensor to cloud and back places MQTT in the full data path.
- ESP32 with MQTT walks through clients, TLS, topics, reconnection and Home Assistant discovery.
- MQTT vs HTTP compares persistent publish-subscribe with request-response for devices.
- MQTT vs Kafka compares device messaging at the edge with event streaming in the back end.
- November 8, 2026: the opening sentence no longer says that most IoT devices use MQTT, a share no source supports; it now calls MQTT widely used; outcomes rewritten in plain words, technical terms explained where they first appear, and the details on connections, sessions, MQTT 5 and security grouped under a separate heading for readers who want to go deeper
- October 23, 2026: added an "In plain words" summary at the top
- October 22, 2026: corrected after an external review: QoS 1 messages are resent on reconnection with a resumed session, not on a timer; MQTT 5 replaces clean session with Clean Start plus Session Expiry Interval; the example will is now retained; a retained message is the last published state, not a guarantee of the current one; platform limits added (AWS IoT Core has no QoS 2 and no AUTH packet); title made specific
- October 17, 2026: Security section corrected: MQTT carries a username and password in the CONNECT packet and MQTT 5 adds the AUTH packet; only encryption is delegated to TLS; sources linked
How corrections are handled: editorial policy.
Topics: MQTT, protocols, messaging, publish-subscribe