← Lessons

quiz vs the machine

Gold1360

Networking

MQTT For IoT

How a lightweight publish subscribe protocol connects tiny devices.

5 min read · core · beat Gold to climb

Built For Small Devices

Sensors and embedded boards have little memory and weak links. MQTT is a compact publish subscribe protocol designed for exactly these constraints, with a header as small as a couple of bytes.

The Broker Model

Devices do not talk to each other directly. They connect to a central broker.

  • A publisher sends a message to a named topic.
  • A subscriber registers interest in a topic and receives matching messages.
  • The broker routes messages from publishers to subscribers.

Topics form a hierarchy with slashes, and wildcards let a subscriber match a whole branch.

Quality Of Service

MQTT offers three delivery levels so a device can trade reliability for cost.

  • Level zero sends at most once with no guarantee.
  • Level one ensures at least once, possibly duplicating.
  • Level two ensures exactly once through a longer exchange.

Staying Connected Cheaply

A device sends small keepalive pings so the broker knows it is alive. A last will message can be registered so that if a device drops unexpectedly, the broker publishes a notice on its behalf. Retained messages let a new subscriber get the latest value immediately.

Key idea

MQTT connects constrained devices through a broker using topic based publish subscribe, with three quality of service levels and features like last will and retained messages for reliability.

Check yourself

Answer to earn rating on the learn ladder.

1. How do MQTT devices exchange messages?

2. What does MQTT quality of service level two guarantee?

3. What is the purpose of a last will message?