What it is
MQTT is a lightweight messaging protocol based on publish and subscribe. Devices connect to a broker and publish messages to named topics, such as a path naming a machine and a reading. Anything subscribed to that topic gets the message. Publishers and subscribers never talk to each other directly. MQTT was created at IBM in the late 1990s for monitoring equipment over unreliable links, and it is now an OASIS standard.
It is built for small devices and poor networks. Headers are tiny, connections stay open, and three quality of service levels let you choose between at most once, at least once and exactly once delivery. A last will message lets the broker announce when a device drops off unexpectedly, and retained messages give new subscribers the latest value straight away. It normally runs over TCP, secured with TLS.
How I use it
- In the pattern I build with, each device publishes readings such as power-on, usage and location to its own MQTT topic.
- AWS IoT Core acts as the broker, and rules on those topics route the data on to storage, alerts and dashboards.
- Devices connect over TLS with their own certificates, and a policy limits each one to its own topics.
- Small messages and persistent connections suit Raspberry Pi devices on cellular modems, where data and signal are limited.
MQTT is the right tool when many small devices need to report often over links that drop, such as cellular on a job site. It uses less data and power than polling a web API, and the broker decouples devices from the apps that read them. I use it because it is what AWS IoT Core speaks natively, so a Pi on a machine can publish a reading and AWS takes it from there.
Questions
Why use MQTT instead of HTTP for IoT devices?
MQTT keeps one small connection open and sends tiny messages, which saves data and power on devices with weak signal. It also lets many apps subscribe to the same readings without each device knowing about them.
How do you use MQTT with AWS?
AWS IoT Core is the broker. In the pattern I build with, a device publishes readings to its own topic over TLS, and IoT rules send those messages on to the AWS services that store and display them.