A tiny publish-subscribe protocol for IoT devices with weak batteries and weak networks.
MQTT is a very small publish/subscribe protocol made for devices with weak batteries and weak networks, such as sensors, meters and smart-home gadgets.
home/kitchen/temp. Anyone who subscribed to that topic gets it.MQTT (often read as "em-queue-tee-tee") runs on top of a normal TCP connection. A device opens one connection to the broker and keeps it open. Over that single connection it can both publish messages and subscribe to topics. The fixed header of a message can be as small as 2 bytes, which matters when every byte costs battery and mobile data.
The pieces are simple. A publisher sends a message to a topic (a text path with levels split by slashes). A subscriber tells the broker which topics it cares about, using a filter. The broker matches each message against all filters and forwards copies. The publisher and the subscriber never know about each other.
Topics have levels separated by /. A subscriber can use two wildcards in its filter. + matches exactly one level, and # matches everything below (and it must be the last character). A publisher always uses a full, exact topic with no wildcards.
| Filter | Matches | Does not match |
|---|---|---|
| home/kitchen/temp | Only that exact topic | home/kitchen/light |
| home/+/temp | home/kitchen/temp, home/garage/temp | home/kitchen/oven/temp (two levels in the middle) |
| home/# | Everything under home, and home itself | farm/field1/soil |
| # | Every topic on the broker | Nothing is left out (careful, this can be a lot) |
At most once. Can be lost.
At least once. Can repeat.
Exactly once. Most packets.
| Level | Name | How it works | Risk | Cost |
|---|---|---|---|---|
| QoS 0 | At most once | Send once. No confirmation. | A message can be lost | 1 packet. Cheapest. |
| QoS 1 | At least once | Send, wait for PUBACK, repeat if it does not come. | A message can arrive twice | 2 packets (more with retries) |
| QoS 2 | Exactly once | A 4-step handshake: PUBLISH, PUBREC, PUBREL, PUBCOMP. | No loss, no duplicates | 4 packets. Slowest. |
| Feature | What it does | Why it helps |
|---|---|---|
| Retained message | The broker keeps the last message of a topic and gives it to every new subscriber at once. | A new dashboard shows the current state straight away, without waiting for the next reading. |
| Last Will and Testament | A message the broker publishes for you if your connection dies without a clean goodbye. | Others learn that a device went offline (for example โdevice 7: offlineโ). |
| Persistent session | The broker remembers your subscriptions and queues messages for you while you are away (QoS 1 and 2). | A device that reconnects after a gap catches up without resubscribing. |
| Keep-alive | The client pings if it has been quiet. If the broker hears nothing, it closes the connection. | Dead connections are noticed and cleaned up. |
QoS 1, at least once: the sender repeats the PUBLISH until it sees a PUBACK. Nothing is lost, but a lost PUBACK causes a duplicate.
Wildcards follow the real MQTT rules: + matches exactly one level and # matches everything below (and the parent level itself, so home/# also matches home). The loss pattern is a fixed list, not random, and the message counts are illustrative. In real MQTT the QoS used is the lower of the publisher's and the subscriber's level.
# subscribe to every temperature in the house
subscribe filter="home/+/temp" qos=1
# publish a reading
publish topic="home/kitchen/temp" payload={"c":21.5} qos=0 retain=false
# connect with a last will
connect clientId="sensor-7" will.topic="devices/sensor-7/status" will.payload="offline" will.retain=truehome/#, but you cannot publish to it. A publish needs a full topic. Also avoid subscribing to # in production, since you receive everything.| Term | Meaning in easy words |
|---|---|
| Broker | The server that receives all messages and forwards them to matching subscribers |
| Publisher | A client that sends messages to a topic |
| Subscriber | A client that asks the broker for messages that match a topic filter |
| Topic | A text path with levels split by slashes, such as home/kitchen/temp |
| Wildcard | + matches one level and # matches all the levels below |
| QoS | Quality of Service: the delivery guarantee (0, 1 or 2) |
| Retained message | The last message on a topic, stored by the broker for new subscribers |
| Last Will and Testament | A message the broker publishes if a client disconnects without saying goodbye |
| Persistent session | Subscriptions and queued messages that the broker keeps while the client is away |
| Keep-alive | A timer and ping that lets the broker notice a dead connection |
| Client ID | The name that identifies a client to the broker and ties it to a session |
| Choice | We gain | We lose |
|---|---|---|
| QoS 0 | Fewest packets and lowest power | Lost messages go unnoticed |
| QoS 1 | No loss, simple to do | Duplicates, so handlers must be idempotent |
| QoS 2 | No loss and no duplicates | More packets, more delay, more state on the broker |
| Persistent session | Devices catch up after a gap | The broker stores more, and old sessions need cleaning up |
| Retained messages | New subscribers see the latest state at once | A stale value can mislead if a device died silently (pair it with a last will) |
| MQTT vs HTTP polling | Push, small, long-lived connection | Needs a broker, and some firewalls dislike long-lived connections |
| MQTT | HTTP | AMQP (RabbitMQ) | |
|---|---|---|---|
| Style | Publish/subscribe | Request/response | Queues, exchanges and routing |
| Weight | Very light | Heavier headers | Richer, heavier |
| Best for | Devices, telemetry | Web and APIs | Business messaging between services |
| Server push | Built in | Needs tricks (SSE, WebSockets) | Built in |