Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Publish and subscribe with MQTT topics

After this lesson: I can explain publish and subscribe.

55 min lesson4 commandsMQTT messaging lab: how devices talk to a broker, in the browser

Publishers and subscribers never meet

Publish and subscribe is a way for programs to share messages without knowing about each other. A publisher sends a message to a topic, a subscriber listens to a topic, and a broker passes each message between them.

This is how one door sensor can tell a phone, a logger and an alarm at once. None of them is wired to it.

By the end of this lesson

Note

I can explain publish and subscribe.

Key words

WordWhat it meansExample
Publishera program that sends messages to a topic
Subscribera program that listens to topics
Topic filterthe pattern a subscriber uses to choose topicshome/+/temperature
Single-level wildcardthe symbol + that matches exactly one level
Multi-level wildcardthe symbol # that matches every level below

The cells students fill in are left blank in this preview.

The broker sits between them

A publisher does not know who is listening. A subscriber does not know who is sending. Each of them talks only to the broker.

When a message arrives, the broker looks at every subscription and passes the message to each subscriber whose filter fits the topic. If nobody is listening, the message is simply not delivered.

Publish a retained message of your own

Command
docker exec broker mosquitto_pub -t home/notes/lecture -r -m 'hello from the class'

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Subscribe and read it back

Command
docker exec broker mosquitto_sub -t home/notes/lecture -v -C 1 -W 3

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Your own message comes back the same

The first command prints nothing. -t is the topic, -m is the payload and -r makes the message retained. The topic is one you made up, so nobody else is writing to it.

The second prints exactly home/notes/lecture hello from the class. You were a publisher, then a subscriber, and the broker held the message between.

A topic filter chooses what you hear

A subscriber says which topics it wants with a topic filter. A filter with no symbols matches one topic only.

The symbol + matches exactly one level. So home/+/temperature matches home/kitchen/temperature but not home/temperature. The symbol # matches everything below, and it may only be the last level.

Worked example: which filters match a topic

Filterhome/kitchen/temperaturehome/door/stategarden/soil/moisture
home/#YesYesNo
home/+/temperatureYesNoNo
home/+NoNoNo
#YesYesYes
home/door/stateNoYesNo

The cells students fill in are left blank in this preview.

Listen to one room, any sensor

Command
docker exec broker mosquitto_sub -t 'home/livingroom/+' -v -C 4 -W 10

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A filter changes which lines you see

This asks for every sensor in the living room. You see lines that start with home/livingroom/temperature or home/livingroom/humidity, each with a whole number. The kitchen and the door never appear, because their topics do not fit.

The order and the numbers depend on when you press Run.

A common mistake

Warning

Many people think a subscriber asks the sensor for a reading. It does not. The sensor publishes when it likes, and the broker passes the message to whoever is subscribed at that moment. That is why a retained message exists: it is how a late subscriber still learns the last value.

Ask for the door state

Command
docker exec broker mosquitto_sub -t home/door/state -v -C 1 -W 5

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A retained message answers a late subscriber

The door sensor marks its messages retained, so this command answers at once with one line: home/door/state and then open or closed. The broker already held the last message.

A topic that is not retained would make you wait until the sensor next publishes. Retained does not mean kept for ever: the next message on the topic replaces it.

Match one level at a time

Code: python
topic = "home/kitchen/temperature"
pattern = "home/+/temperature"
a = topic.split("/")
b = pattern.split("/")
same = len(a) == len(b)
for i in range(len(b)):
    if same and b[i] != "+" and b[i] != a[i]:
        same = False
# Change the pattern to home/+/humidity, then press Run.
print(same)

About this lab and its source: MQTT messaging lab: how devices talk to a broker, in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.