Guides › What Is MQTT? Brokers, Topics, QoS, Retain and Topic Design
What Is MQTT? Brokers, Topics, QoS, Retain and Topic Design
MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol for exchanging small messages between sensors, gateways and servers. It runs over TCP/IP with a header as small as 2 bytes, which suits narrow or unreliable networks. It was created in 1999 for remote monitoring of oil pipelines and is now an OASIS standard (v3.1.1 and v5.0) and the de facto IoT protocol.
Publish/subscribe
Unlike HTTP, clients do not talk to each other directly. A broker in the middle routes messages.
- Publisher: sends a message to a topic, e.g. a sensor publishes
23.5tofactory/line1/temp. - Subscriber: subscribes to topics of interest; the broker delivers matching messages.
- Broker: receives and distributes messages. Examples: Mosquitto, EMQX, HiveMQ, AWS IoT Core.
Publishers and subscribers do not need to know about each other, so adding sensors or dashboards never requires changing existing devices.
Topics and wildcards
A topic is a hierarchical, case-sensitive string separated by slashes.
factory/line1/machine3/temperature factory/line1/machine3/status home/livingroom/light/set
Wildcards are allowed only when subscribing.
| Wildcard | Meaning | Example | Matches |
|---|---|---|---|
+ | Exactly one level | factory/+/machine3/temperature | machine3 temperature on every line |
# | All remaining levels (last position only) | factory/line1/# | Everything under line1 |
Topics starting with $SYS/ are reserved for broker statistics.
Topic design tips
- Go from broad to narrow:
site/area/device/measurement - Do not start with a slash (
/factorycreates an empty first level) - Avoid spaces and special characters; prefer lowercase and hyphens
- Separate commands from state:
.../light/set(command),.../light/state(state) - Put device IDs in the topic so wildcards can group them
QoS (Quality of Service)
| QoS | Guarantee | Mechanism | Use |
|---|---|---|---|
| 0 | At most once | Fire and forget | Frequent sensor readings where losing one is fine |
| 1 | At least once | PUBACK; resent if not acknowledged, so duplicates are possible | Most data, alarms |
| 2 | Exactly once | Four-step PUBREC, PUBREL, PUBCOMP handshake | Billing or commands that must not repeat |
Higher QoS means more round trips and latency. The effective QoS is the lower of the publish QoS and the subscription QoS.
Retained messages
When a message is published with the retain flag, the broker stores the last message on that topic and sends it immediately to any new subscriber. A freshly opened dashboard shows the current state without waiting for the next update. To clear it, publish an empty retained payload to the same topic.
Last Will and Keep Alive
A client can register a Last Will message when it connects. If the client disappears without a clean disconnect (power loss, network failure), the broker publishes that message on its behalf, for example offline to factory/line1/machine3/status. If no packet arrives within the keep-alive period (in practice 1.5 times the keep-alive value), the broker treats the connection as dead.
Sessions (Clean Session / Clean Start)
With Clean Session turned off (in v5, Clean Start = false plus a session expiry interval), the broker keeps subscriptions and undelivered QoS 1/2 messages, so a reconnecting client receives what it missed. The client must reconnect with the same Client ID. If two clients connect with the same Client ID, the earlier one is disconnected.
Ports and security
| Port | Use |
|---|---|
| 1883 | MQTT, unencrypted |
| 8883 | MQTT over TLS |
| 8083/8084, 443, etc. | MQTT over WebSocket (varies by broker), for browsers |
- Always use TLS (8883) and authentication on brokers reachable from the internet.
- Use ACLs to limit which topics each client may publish and subscribe to.
- A broker that allows anonymous access lets anyone read messages and send commands.
Payloads and encoding
MQTT does not interpret payloads; it delivers bytes as-is (up to 256 MB). Text, JSON and binary are all fine.
{"device":"machine3","temp":23.5,"unit":"C","ts":1764400000}
- JSON: readable and extensible; supported everywhere
- Binary: compact, good for low-power, low-bandwidth devices; inspect it as HEX when debugging
- Base64: for putting binary data into text-only places (logs, JSON fields)
This site's MQTT message builder sets topic, QoS and retain and encodes the payload as HEX or Base64. See the encoding guide and the JSON guide.
MQTT compared
| Item | MQTT | HTTP REST | Modbus TCP |
|---|---|---|---|
| Model | Publish/subscribe | Request/response | Request/response (polling) |
| Real-time push | Yes | Needs polling | Needs polling |
| Overhead | Very small | Large (headers) | Small |
| Typical use | IoT, remote monitoring, cloud | Web APIs | Plant equipment control |
A common architecture reads plant equipment over Modbus and has a gateway publish the values to the cloud over MQTT.
Try it now: MQTT Message Builder (runs in your browser, no data is sent anywhere)