MQTT/The post office/Topics

Topics

An MQTT message is addressed to a topic, a name for what it is about, never to a device. The broker sorts by that name alone, and a single wrong capital sends a reading where nobody is listening.

A letter to the post office in the last lesson had no person's name on it. What it carried instead is a topic: a name for what the message is about, such as home/kitchen/temp. The sensor posts to a topic; readers ask for topics. Nobody names anybody.

Levels

The slashes split a topic into levels, the way folders split a path: home, then kitchen, then temp. The broker does not care what the levels mean. They matter to the readers, who can ask for a whole level at once — that is lesson four.

There is nothing to create. The first message posted to home/garage/temp is what makes that topic exist, and a topic that nobody posts to and nobody asks for costs nothing at all.

A wall of wooden pigeonholes with blank brass plates, one lit orange with an envelope in it
A pigeonhole for each topic, made by the first letter posted to it.Generated illustration

Exact, to the letter

The broker compares topics character by character, so Home/kitchen/temp and home/kitchen/temp are two different topics. So are home/kitchen/temp and home/kitchen/temp/ with a slash on the end, and home/kitchen temp with a space in it. None of these is an error: each is a perfectly good topic that nobody happens to be listening to, and the reading goes nowhere without a word.

The payload

What is inside the envelope is the payload, and the broker never opens it. It is bytes: 21.4, on, a line of JSON, a photograph. MQTT has no opinion about units or format. The sender and the reader agree what it means, and it is worth writing that agreement down next to the topic — home/kitchen/temp, degrees Celsius, one decimal place.

Common mistakes

  • A capital letter, or a trailing slash, in one program and not the other. Copy the topic string from one place into both, rather than typing it twice.
  • Putting the reading in the topic, as in home/kitchen/temp/21.4. Every reading becomes a new topic nobody subscribes to. The topic says what, the payload says how much.
  • Guessing a unit. A payload of 21 is Celsius to one program and Fahrenheit to another; neither will complain.

Edit this page — content/fundamentals/mqtt/topics.mdx

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Fundamentals on the forum →