MQTT is a lightweight messaging protocol designed for M2M (machine-to-machine) communication and IoT applications. It's highly bandwidth-efficient, easy to integrate, and built on top of TCP for reliable communication over unreliable channels. As a binary protocol, it's meant for machines and allows messages as small as 2 bytes, making it ideal for constrained devices.
It supports bi-directional, data-agnostic, push-style communication — where messages are pushed from publishers to subscribers via a broker, without the clients needing to poll for updates. TLS can be enabled for security, and it scales well due to its publish/subscribe (pub/sub) model.
In the pub/sub model, clients publish data to a central broker under specific topics. Other clients subscribe to these topics to receive updates. This architecture offers:
The broker acts as the message hub. It manages all subscriptions and message routing, making scalability a strong point. However, this also means the broker is a single point of failure (SPOF). Clients typically use MQTT libraries or SDKs to communicate with a broker service.
Topics are UTF-8 strings used by the broker to route messages. They can have multiple hierarchical levels, separated by slashes (e.g., sensor/temp
). Wildcards are supported:
#
- Matches all sub-topics (e.g., sensors/#
matches all topics under sensors
)+
- Matches exactly one level (e.g., sensors/+/temp
matches sensors/room1/temp
)MQTT offers three levels of Quality of Service to control delivery guarantees:
PUBACK
.PUBLISH
PUBREC
PUBREL
PUBCOMP