MQTT
ActiveMQTT (Message Queuing Telemetry Transport) is a lightweight publish-subscribe messaging protocol designed for constrained devices and unreliable networks. An MQTT broker decouples publishers from subscribers – publishers send messages to topics, subscribers receive them without direct connection to publishers. MQTT is the dominant protocol for IoT device communication and is an OASIS standard.
In one line
MQTT 5.0 (OASIS Standard, March 2019) is a binary publish-subscribe messaging protocol over TCP. Publishers send payloads to named topics on a broker; subscribers receive all messages matching their topic filters. Three Quality of Service levels: QoS 0 (at most once), QoS 1 (at least once), QoS 2 (exactly once). MQTT runs on port 1883 (plaintext) and port 8883 (TLS). MQTT over WebSocket uses port 443 or 9001. The protocol is the dominant IoT standard – used in AWS IoT, Azure IoT Hub, Home Assistant, and HiveMQ.
Quick Reference
| Field | Size | Description |
|---|---|---|
| Port 1883 | TCP | Standard MQTT over plaintext TCP. IANA assigned. Should only be used on trusted internal networks. |
| Port 8883 | TCP + TLS | MQTT over TLS. IANA assigned. Required for internet-facing deployments. TLS 1.2+ recommended, TLS 1.3 preferred. |
| Port 443/9001 | WebSocket | MQTT over WebSocket for browser clients and firewall-friendly deployments. Port 443 for TLS, 9001 for plaintext. |
| Fixed header | 2 bytes min | Byte 1: packet type (4 bits) + flags (4 bits). Byte 2+: remaining length (variable-length encoding, 1–4 bytes). |
| QoS 0 | At most once | Fire and forget. No acknowledgement, no retry. Fastest, lowest overhead. Used for sensor readings where occasional loss is acceptable. |
| QoS 1 | At least once | Publisher retries until PUBACK received. Message may be delivered multiple times. Consumer must be idempotent. |
| QoS 2 | Exactly once | Four-part handshake: PUBLISH → PUBREC → PUBREL → PUBCOMP. Guaranteed exactly once delivery. Highest overhead. |
| Topic syntax | UTF-8 string | Hierarchical with / separator: home/kitchen/temp. Wildcards: + (single level), # (multi-level, must be last). # alone subscribes to all topics. |
| Retain flag | 1 bit | Broker stores the last retained message per topic. New subscribers immediately receive the retained message without waiting for the next publish. |
| Will message | Optional | Defined at CONNECT time. Broker publishes the Will message if the client disconnects ungracefully. Used for device offline notifications. |
| Keep-alive | seconds | Client MUST send PINGREQ within the keep-alive interval or the broker closes the connection. Broker responds with PINGRESP. |
| Clean session (5.0) | Session expiry interval | MQTT 5.0 replaces the boolean clean session flag with a Session Expiry Interval in seconds. 0 = session ends at disconnect. 0xFFFFFFFF = session never expires. |
Key Characteristics
Publish-subscribe decoupling
Publishers and subscribers never connect directly. The broker routes messages. A publisher does not know who is subscribed; a subscriber does not know who published.
Minimal overhead
The fixed header is 2 bytes minimum. PUBLISH with QoS 0 and a short topic has less than 10 bytes overhead. Designed for microcontrollers with 256 bytes of RAM.
Will message
The Last Will and Testament (LWT) message lets a broker notify subscribers when a client disconnects ungracefully – essential for device status monitoring in IoT.
QoS mismatch
The effective QoS of delivery is min(publisher QoS, subscriber QoS). A QoS 2 publish to a QoS 0 subscriber delivers at most once. Mismatches are a common source of unexpected message loss.
Message Format
# MQTT CONNECT packet (binary, shown as hex + annotation)
# Fixed header
10 # Packet type 1 (CONNECT), flags 0000
2C # Remaining length = 44 bytes
# Variable header
00 04 4D 51 54 54 # Protocol Name: "MQTT"
05 # Protocol Version: 5 (MQTT 5.0)
02 # Connect Flags: Clean Start=1, Will=0
00 3C # Keep Alive: 60 seconds
00 # Properties length: 0
# Payload
00 08 6D 79 2D 64 65 76 69 63 65 # Client ID: "my-device"
# CONNACK response from broker
20 09 # Packet type 2 (CONNACK)
00 # Connect Acknowledge Flags
00 # Reason Code: 0x00 = Success
06 # Properties length: 6
27 00 00 04 00 # Maximum Packet Size: 1024# MQTT PUBLISH (QoS 1) – broker to subscriber
32 # Packet type 3 (PUBLISH), QoS 1, DUP=0, RETAIN=0
20 # Remaining length = 32
# Variable header
00 12 68 6F 6D 65 2F 6B 69 74 63 68 65 6E 2F 74 65 6D 70
# Topic: "home/kitchen/temp" (length 18)
00 01 # Packet Identifier: 1 (QoS 1 requires this)
00 # Properties length: 0
# Payload
32 33 2E 35 # "23.5" – temperature reading
# Subscriber ACK
40 02 00 01 00 # PUBACK: type 4, remaining 2, packet ID 1, reason 0x00 (Success)
# Topic wildcard examples
home/+/temp # matches home/kitchen/temp, home/bedroom/temp
home/# # matches home/, home/kitchen/temp, home/kitchen/humidity
$SYS/# # broker system topics (retained stats, uptime, connected clients)