DevTools Hub

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.

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.

WildcardMeaningExampleMatches
+Exactly one levelfactory/+/machine3/temperaturemachine3 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

QoS (Quality of Service)

QoSGuaranteeMechanismUse
0At most onceFire and forgetFrequent sensor readings where losing one is fine
1At least oncePUBACK; resent if not acknowledged, so duplicates are possibleMost data, alarms
2Exactly onceFour-step PUBREC, PUBREL, PUBCOMP handshakeBilling 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

PortUse
1883MQTT, unencrypted
8883MQTT over TLS
8083/8084, 443, etc.MQTT over WebSocket (varies by broker), for browsers

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}

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

ItemMQTTHTTP RESTModbus TCP
ModelPublish/subscribeRequest/responseRequest/response (polling)
Real-time pushYesNeeds pollingNeeds polling
OverheadVery smallLarge (headers)Small
Typical useIoT, remote monitoring, cloudWeb APIsPlant 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)