Wireless Notes
Learn MQTT protocol with publish subscribe model, QoS levels 0 1 2, topics hierarchy, retained messages, last will testament, lightweight design, and comparison with HTTP for IoT engineering students.
Understanding MQTT protocol architecture, message flow, topic design, Quality of Service levels, retained messages, last will testament, and why MQTT dominates IoT communication.
The Publish-Subscribe Model
Why Not Request-Response?
In HTTP, a client must request data from a specific server. This creates tight coupling — the client must know the server's address, the server must be available when the client asks, and scaling requires the server to handle increasing load. For IoT with thousands of sensors and multiple consumers, this model breaks down.
MQTT uses publish-subscribe (pub-sub): producers publish messages to named topics without knowing who (if anyone) will receive them. Consumers subscribe to topics of interest and receive messages automatically when published. A central broker handles all routing.
| [Temperature Sensor] ──publishes── | "factory/line3/temp" ──→ [MQTT Broker] ──→ [Dashboard] |
| ── | [Alert System] |
| ── | [Data Logger] |
Decoupling Benefits
| Dimension | Decoupled Aspect |
|---|---|
| Space | Publisher and subscriber don't need each other's network address |
| Time | They don't need to be online simultaneously |
| Synchronization | Operations are asynchronous — neither blocks waiting for the other |
MQTT Protocol Details
Connection Lifecycle
- CONNECT — Client sends connection request with client ID, optional username/password, keepalive interval, and optional Will message
- CONNACK — Broker acknowledges connection (success or failure code)
- SUBSCRIBE — Client subscribes to topics of interest
- PUBLISH/RECEIVE — Messages flow based on publications and subscriptions
- PINGREQ/PINGRESP — Keepalive heartbeat when no messages flowing
- DISCONNECT — Clean disconnection
Topic Structure
Topics are UTF-8 strings using forward-slash hierarchy:
Topic Wildcards
Subscribers can use wildcards:
- + (single level):
building/+/room301/temperaturematches floor1, floor2, floor3... - # (multi-level):
vehicle/truck42/#matches ALL topics under truck42 - Combined:
building/+/+/temperaturematches temperature in any room on any floor
Quality of Service (QoS) Levels
QoS 0: At Most Once (Fire and Forget)
- Fastest, lowest overhead (no ACK packets)
- Message may be lost if network drops during transmission
- Use for: Frequent sensor readings where losing one is harmless (next reading comes soon)
QoS 1: At Least Once
- Guaranteed delivery (retransmits until acknowledged)
- May deliver duplicates (if ACK is lost, sender retransmits)
- Use for: Important events where duplicates are acceptable (alerts, commands)
QoS 2: Exactly Once
- Guaranteed single delivery (no loss, no duplicates)
- Highest overhead (4 packets per message delivery)
- Use for: Financial transactions, billing events, critical one-time commands
QoS Comparison
| QoS | Delivery Guarantee | Packets per Message | Latency | Use Case |
|---|---|---|---|---|
| 0 | At most once | 1 | Lowest | Sensor telemetry |
| 1 | At least once | 2 | Medium | Alerts, commands |
| 2 | Exactly once | 4 | Highest | Billing, safety-critical |
Retained Messages
When a message is published with the retain flag set, the broker stores the last message on that topic. Any new subscriber immediately receives this retained message upon subscribing — they do not have to wait for the next publication.
Without retain: New dashboard connects → sees nothing until sensor publishes next reading (could be 30 seconds) With retain: New dashboard connects → immediately receives current temperature (retained from last publication)
This solves the "late joiner" problem and ensures the system state is always available to new subscribers.
Last Will and Testament (LWT)
When connecting, a client can register a "will" message with the broker. If the client disconnects unexpectedly (network failure, crash — NOT a clean DISCONNECT), the broker automatically publishes the will message on the client's behalf.
Typical LWT:
- Topic:
device/sensor42/status - Message:
"offline" - Retain: true
This enables automatic device health monitoring. The dashboard subscribes to status topics and immediately knows when any device goes offline, without polling.
MQTT vs Other IoT Protocols
| Feature | MQTT | HTTP | CoAP | AMQP |
|---|---|---|---|---|
| Transport | TCP | TCP | UDP | TCP |
| Min header size | 2 bytes | 100+ bytes | 4 bytes | 8+ bytes |
| Pub/Sub | Native | No (polling) | Observe pattern | Yes |
| QoS levels | 3 | No | Confirmable/Non | Yes |
| Retained messages | Yes | No | No | Yes |
| Last Will | Yes | No | No | No |
| Battery efficiency | Excellent | Poor | Good | Medium |
| Bidirectional | Yes | Request only | Yes | Yes |
| Broker needed | Yes | No | No (optional) | Yes |
| Adoption in IoT | Dominant | Legacy devices | Constrained devices | Enterprise |
MQTT in Practice
Popular Brokers
| Broker | Type | Max Connections | Best For |
|---|---|---|---|
| Eclipse Mosquitto | Open-source, lightweight | 100K+ | Edge, development, small deployments |
| EMQX | Open-source, clustered | 100M+ | Enterprise, large-scale IoT |
| HiveMQ | Commercial | 10M+ | Mission-critical enterprise |
| AWS IoT Core | Cloud-managed | Unlimited | AWS ecosystem |
| Azure IoT Hub | Cloud-managed | Unlimited | Azure ecosystem |
Real-World Deployments
- Facebook Messenger — Uses MQTT for mobile push notifications (billions of devices)
- AWS IoT — MQTT is the primary device communication protocol
- Home Assistant — Smart home platform uses MQTT for device integration
- Tesla vehicles — Report telemetry to cloud via MQTT
- Industrial SCADA — Sparkplug B specification standardizes MQTT for industrial use
Key Takeaways
- MQTT's publish-subscribe model decouples message producers from consumers in space, time, and synchronization — ideal for dynamic IoT environments
- The minimum 2-byte header makes MQTT extremely bandwidth-efficient compared to HTTP's 100+ byte headers — critical for constrained networks
- Three QoS levels (0, 1, 2) provide explicit trade-offs between delivery guarantee and protocol overhead — choose per message type
- Retained messages solve the late-joiner problem by immediately delivering current state to new subscribers without waiting for next publication
- Last Will and Testament enables automatic offline detection — the broker publishes on behalf of crashed clients
- MQTT runs over TCP, providing reliable byte-stream delivery that simplifies client implementation on constrained devices
- MQTT dominates IoT messaging because it was purpose-designed for the constraints IoT devices face: limited bandwidth, unreliable connections, and battery power
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for MQTT Protocol IoT Messaging Publish Subscribe QoS.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Wireless Communications topic.
Search Terms
wireless-communications, wireless communications, wireless, communications, iot, and, mqtt, protocol
Related Wireless Communications Topics