Skip to content
LSLab

How Does MQTT Route Messages Through a Broker?

  • Basic
  • Article
  • Ages 15+
  • 4 min read
An air-quality sensor, two-antenna Wi-Fi gateway and tablet are arranged together on a lab bench.

This article describes MQTT Version 5.0. MQTT is a lightweight protocol that moves messages by publish and subscribe. A device publishes a message with a topic, and the broker in the middle receives it and passes it to the devices subscribed to that topic. The parties need not know each other.

Contents
One side publishes and the broker passes it to many subscribers.
  • Publisher
  • Broker
  • Subscribers

What you will learn

Before you start

It helps to know how IoT devices connect to a network.

How does a broker pass messages on?

MQTT is a Client Server publish/subscribe messaging transport protocol. The Server is a program or device that acts as an intermediary between Clients which publish messages and Clients which have made Subscriptions.

The broker routes messages to the appropriate subscribers based on the topic hierarchy. MQTT is light weight, open, simple and designed to be easy to implement.

The broker only passes the message to the party subscribed to the right topic.
  • Publisher
  • Message with a topic
  • Broker
  • Subscriber to the topic
  • Party not subscribed

How do topics and wildcards work?

A topic name is the label attached to a message, matched against the subscriptions the server knows. A topic filter is an expression in a subscription that shows interest in one or more topics.

The forward slash separates topic levels. The + wildcard matches exactly one level and must occupy a whole level in a filter. The # wildcard matches the parent level and any number of child levels; it must appear alone or after a /, and be the final character in a filter.

Topics form a tree; the + wildcard gathers matching topics at one level.
  • Root of the topic tree
  • One topic level
  • The + wildcard gathers topics at one level

How do the three Quality of Service levels differ?

MQTT has three Quality of Service levels: QoS 0 (at most once) uses best effort; QoS 1 (at least once) assures delivery but may produce duplicates; QoS 2 (exactly once) assures one delivery. These levels describe each delivery hop from one sender to one receiver; the broker treats each recipient independently.

The higher the level, the more confirmation rounds.
  • At most once: send only
  • At least once: with confirmation
  • Exactly once: multi-step confirmation

MQTT also decouples by time, allowing the broker to store messages for clients that are not online, while the session and subscription remain valid. Storage also depends on message QoS, message expiry and broker policy. If a client configured a Will Message, the server can publish it after an abnormal connection closure; timing also depends on Will Delay and the session.

Real-world example

With an adult, think of a temperature sensor at home. The sensor publishes a reading to a topic. A phone and a screen in the house both subscribe to that topic, so both receive the reading.

Common misunderstandings

Devices must connect straight to each other. With publish/subscribe, applications are decoupled and go through the broker.

Every message arrives exactly once. That is only one of three levels; at least once can produce duplicates.

Frequently asked questions

What is MQTT?

MQTT is a light, simple publish/subscribe messaging transport protocol.

What does a broker do?

The broker routes messages to the appropriate subscribers based on the topic hierarchy.

What are wildcards for?

The + wildcard matches exactly one topic level and must occupy that whole level in a filter; # matches the parent level and any number of child levels, must appear alone or after a /, and must be the final character in a filter.

What is a will message?

A message configured by a client for the server to potentially publish after an abnormal closure, depending on the delay and session state.

Summary

  • MQTT uses publish and subscribe, with a broker routing messages by topic.
  • Topics are layered with forward slashes, and wildcards match several topics.
  • QoS 0 is at most once, QoS 1 is at least once, and QoS 2 is exactly once for each delivery hop between one sender and one receiver.

Terms in this article

English and Vietnamese terms, with the meanings used here. Hover, tap or Tab to a term with a dotted underline for an explanation as you read.

MQTT
A lightweight protocol that moves messages by publish and subscribe between clients and a server.
Broker
The intermediary server that receives messages and passes them to matching subscribers.
TopicChủ đề
A label attached to a message; / separates a topic name into levels.
WildcardKý tự thay thế
A character in a topic filter: + matches one whole level; # matches the parent and child levels and appears at the end of the filter.
Quality of ServiceChất lượng dịch vụ
The delivery guarantee level: at most once, at least once or exactly once.
Will messageBản tin di chúc
A message configured by a client for the server to potentially publish after abnormal closure, depending on delay and session state.

This article is not marked as viewed

Spotted something wrong or unsafe?

Tell the administrator by email. The message already has the article title and address.

Report this article
  • A microcontroller development board is connected by USB with its amber status LED illuminated.
    Basic

    How Does Arduino Run setup and loop?

    An Arduino sketch has two functions: setup runs once and loop repeats forever. See the run order, why loop never pauses and what delay does.

    Internet of Things (IoT)4 min read
  • A compact microcontroller board with a metal shield is wired to a temperature sensor.
    Basic

    How Does an ESP32 Board Run and Communicate?

    An ESP32 board pairs an ESP32 module with a USB port and pin headers. See its parts, how firmware is loaded over serial, and its Wi-Fi and Bluetooth.

    Internet of Things (IoT)4 min read
  • A full-size microcontroller development board and a small wireless module sit side by side.
    Starter

    Overview of ESP32, Arduino and Other Boards

    An overview of ESP32 and Arduino boards and how to choose by connectivity, voltage, power and microcontroller.

    Internet of Things (IoT)4 min read
  • An industrial controller and a DIN-rail digital meter are connected by cable on a test bench.
    Basic

    How Does Modbus RTU Exchange Data?

    Modbus RTU works by request and response between a master and slaves. See addresses, function codes, error checks and how frames are split by silent gaps.

    Internet of Things (IoT)4 min read
See all articles of the topic

Sources

These sources are written for older readers. If they feel hard, read them with an adult.