IoT Bricks Blog Try challenge Sign in

Why MQTT, and not HTTP, for devices

Most developers reach for HTTP first. It works, it is everywhere, and for a phone app it is the right tool. For a fleet of small devices on poor networks, MQTT was designed around different constraints.

Publish/subscribe decouples everyone

With HTTP, a device must know the address of the server that wants its data. With MQTT, a device publishes to a topic on a broker and does not care who listens. Add a second consumer tomorrow, such as an alerting service, and the device does not change.

Topics are hierarchical (site/3/room/12/temperature) and subscribers can use wildcards (site/3/#), so one subscription can follow a whole building. Wildcards are stage 5 of the course.

One connection, tiny packets

An MQTT client opens a TCP connection once and keeps it. A packet has a header of as little as two bytes, so sending a reading costs far less than a full HTTP request with its headers. On a metered cellular link, that adds up. The same open connection also lets the broker push to the device: a command can reach it without the device polling.

You choose how hard to try

MQTT gives three delivery levels, set per message:

Choosing the level per message is a real design decision, and the duplicates of QoS 1 are why the next brick, ingestion, must be idempotent. QoS 1 is stage 6.

Built for flaky networks

Several features exist because devices disappear:

The trade-off

A broker is one more component to run, and the protocol is stateful, so a broker is harder to scale than a stateless HTTP server. In return you get a protocol whose defaults match the physical world. In this course you will write that broker yourself, from the first TCP connection to Last Will, and test it against real MQTT 3.1.1 packets.

The first stage is free to try: accept a connection and let the tests tell you what to fix.