IoT Message Communication Protocols: MQTT, CoAP, AMQP, and XMPP
Introduction: Why Do We Need Special Protocols for IoT?
Normal computer protocols like standard HTTP are heavy and send hundreds of bytes of header text with every single message. If a small battery-powered sensor sent large HTTP packets all day, its battery would die in a few weeks!
To make machine-to-machine communication fast, lightweight, and battery-friendly, four major Application Layer Messaging Protocols are used in IoT:
- MQTT (Message Queuing Telemetry Transport)
- CoAP (Constrained Application Protocol)
- AMQP (Advanced Message Queuing Protocol)
- XMPP (Extensible Messaging and Presence Protocol)
1. MQTT (Message Queuing Telemetry Transport)
MQTT was created by IBM in 1999 to monitor oil pipelines over slow satellite links. Today, it is the most popular protocol in the entire IoT industry!
Publishes to:
room/temp = 28°CRoutes message ➔
Mobile App & AC Controller
Key Characteristics of MQTT:
- Architecture: Uses the Publish - Subscribe model centered around an MQTT Broker (e.g. Mosquitto, EMQX).
- Transport Layer: Runs on top of TCP (port
1883, or port8883for secure TLS encryption). - Tiny Header: The base header size is only 2 bytes!
- Topic Hierarchy: Topics use slashes like website URLs (e.g.,
home/bedroom/fan).- Single-level wildcard (
+):home/+/fan(matches the fan in any room). - Multi-level wildcard (
#):home/#(receives all messages from the entire house).
- Single-level wildcard (
The Three Quality of Service (QoS) Levels in MQTT:
MQTT lets you choose how reliably a message must be delivered:
| QoS Level | Guarantee Name | How it Works | Real-World Use Case |
|---|---|---|---|
| QoS 0 | At most once (Fire and Forget) | Message is sent once without any acknowledgment. If the network drops, the message is lost. | Regular temperature updates sent every 5 seconds. |
| QoS 1 | At least once (Acknowledged) | Sender keeps re-sending until it gets a PUBACK reply. Guarantees delivery, but duplicate messages can happen. |
Battery charge alerts, routine status updates. |
| QoS 2 | Exactly once (4-way Handshake) | Uses a 4-step confirmation handshake. Guarantees the message arrives strictly once with zero duplicates. | Financial transactions, critical medical alarms, billing meters. |
2. CoAP (Constrained Application Protocol)
While MQTT uses Publish-Subscribe over TCP, CoAP brings the simplicity of Web REST APIs (GET, POST, PUT, DELETE) to tiny, low-power microcontrollers!
Key Characteristics of CoAP:
- Standardized by: IETF (RFC 7252).
- Transport Layer: Runs over UDP (port
5683), which avoids the slow connection setup of TCP. - Header Size: Fixed binary header of only 4 bytes!
- Architecture: Client-Server Request-Response model (just like HTTP, but formatted in binary numbers instead of heavy text).
CoAP Message Types:
- Confirmable (CON): Important message that requires an immediate acknowledgment (
ACK) from the receiver. If no ACK arrives, it retransmits automatically. - Non-Confirmable (NON): Sent without asking for any ACK. If a packet is lost in the air, nobody worries (good for routine sensor pings).
- Acknowledgment (ACK): The confirmation packet returned to acknowledge a CON message.
- Reset (RST): Sent when a message was received but could not be understood.
3. AMQP (Advanced Message Queuing Protocol)
- An enterprise-level messaging protocol designed for high-reliability systems like banking and logistics.
- Core Architecture: Built around Exchanges, Bindings, and Message Queues.
- Pros: Strong security, reliable delivery, and guaranteed message ordering.
- Drawback in IoT: Headers are much larger and heavier than MQTT; it is mainly used between cloud servers (like RabbitMQ) rather than on battery-powered sensors.
4. XMPP (Extensible Messaging and Presence Protocol)
- Originally created for instant chat applications (like Google Talk or Jabber).
- Key Feature: Presence Detection — devices can easily broadcast when they come online or go offline.
- Drawback in IoT: Uses verbose XML text tags, which creates heavy network traffic and quickly drains battery on small microcontrollers.
5. Summary Comparison Table
| Feature | MQTT | CoAP | AMQP | XMPP |
|---|---|---|---|---|
| Communication Model | Publish - Subscribe | Request - Response (REST) | Pub-Sub & Queues | Pub-Sub & Chat |
| Transport Layer | TCP | UDP | TCP | TCP |
| Header Size | 2 Bytes (Minimal) | 4 Bytes (Compact) | 8+ Bytes | High (Verbose XML) |
| Reliability | 3 QoS Levels (0, 1, 2) | Confirmable (CON) / Non (NON) | Transaction receipts | TCP acknowledgments |
| Best Hardware | Microcontrollers (ESP32, Arduino) | 8-bit constrained sensor motes | Enterprise cloud servers | Smart home gateways / PCs |
💡 DAVV Exam Preparation Tips
5-Mark Question: “Explain MQTT Quality of Service (QoS) levels: QoS 0, QoS 1, and QoS 2 with real-world examples.”
- QoS 0 (At most once): Fire-and-forget; no confirmation, best for regular temperature readings.
- QoS 1 (At least once): Sender retries until confirmed; duplicates possible, best for general alerts.
- QoS 2 (Exactly once): 4-way handshake; zero duplicates guaranteed, best for medical alarms and billing.
10-Mark Question: “Compare MQTT and CoAP messaging protocols in detail. Discuss their architecture (Pub-Sub vs REST), transport layers (TCP vs UDP), header sizes, and message reliability mechanisms.”
- Detail MQTT: Broker architecture, topics, wildcards (
+and#), 2-byte header, runs over TCP.- Detail CoAP: Binary REST over UDP, 4-byte header, CON and NON messages.
- Draw the 4-protocol comparison table (MQTT, CoAP, AMQP, XMPP).
- Conclude with a selection guide: Choose MQTT when you have a persistent internet connection and need Pub-Sub; choose CoAP when devices run on tiny coin-cell batteries over lossy UDP networks.