Skip to content
You Start with one site
Amounts in Start with one site
IoT & Networks · Concept

MQTT publish and subscribe

One idea Intermediate 5 min read
Found this useful?

A notice board in a workshop has a section for each subject. The person who pins a notice does not know who will read it, and readers look only at the sections they care about. MQTT moves data between devices in this way: a device publishes a message to a named topic, and a server called the broker passes it to everyone who has subscribed to that topic.

Every sensor to every program3 × 4 = 12 connections, and each sensor must know every address.Grows fast
Programs ask each sensorEach program must know every sensor, and a sleeping sensor cannot answer.Fails for battery devices
Everyone talks to one broker3 + 4 = 7 connections. Nobody knows anybody but the broker.This is MQTT

See it

Predict first

A sensor publishes a reading to the topic home/garden/moisture. Three programs have subscribed: a display to home/garden/moisture, a phone app to home/garden/# and a laptop script to home/+/moisture. Who receives the reading?

See it: one message, three subscribers

Published to
Filters that match
Delivered to

Choose the topic that the sensor publishes to. The text inside each subscriber is its filter. The broker compares the topic with every filter, level by level, and delivers the message where they match. The sensor sees none of this.

The idea

In MQTT, devices never send to each other. A device publishes a message to a topic, which is a name such as home/garden/moisture. A program that wants those messages subscribes to the topic. The broker is the server between them: it passes each message to every subscriber whose filter matches.

A topic is a row of levels separated by the / sign. A filter may hold two wildcards: + accepts any one level, and # accepts everything below. The sender does not know who the subscribers are, or whether there are any.

One device can do both. A sensor publishes its readings and also subscribes to a topic for commands, such as a new measuring interval. The lesson on MQTT and cloud communication builds such a device.

MQTT needs a network and a broker, and both can fail. So a decision that must be fast or safe, such as stopping a pump that runs dry, stays inside the device. The broker is for oversight: readings, alerts and records.

One worked number

The question

A site has 3 sensors, and 4 programs need their readings. How many connections are needed if every sensor sends directly to every program, and how many with a broker? What changes when a fifth program is added?

Show the worked solution, 3 steps
  1. Count the direct connections

    Each of the 3 sensors connects to each of the 4 programs: 3 × 4 = 12.

  2. Count the connections with a broker

    Each sensor and each program connects once, to the broker: 3 + 4 = 7.

  3. Add the fifth program

    Direct: 3 more connections, and every sensor must be changed to know the new address. With a broker: 1 more connection, and no sensor is touched.

The mistake to avoid

A message that was published has not necessarily been received by anyone. The broker confirms only that it has the message. If no filter matches, because of one wrong letter in the topic or because nobody has subscribed yet, the broker drops the message and the sender sees no error. Topics are case sensitive: Home and home are different. When a reading is missing, first compare the two topic texts letter by letter.

Check yourself

Question 1

A site has 10 sensors and 3 programs that read all of them. How many connections are needed with a broker, and how many if every sensor sends directly to every program?

Question 2

A program subscribes with the filter site/+/temperature. Which of these topics does it receive?

Question 3

A sensor publishes an alarm, and the broker confirms that it received the message. Can the sensor be sure that the phone of the technician has shown the alarm?

Where you meet it

  • A borehole pump far from the officeThe controller publishes pressure and running hours. The office subscribes and never needs the address of the pump.
  • Cold rooms on several sitesEach room publishes to its own topic, and one filter with a + collects the temperature of every room.
  • A solar plantA gateway publishes the inverter readings. The recording program and the alarm program both subscribe, and neither knows the other.
  • A generator on standbyThe broker can announce that a device has gone silent, so a dead link is not mistaken for a quiet machine.
Key points
  • Devices publish to a topic. They do not send to each other.
  • The broker passes each message to every subscriber whose filter matches.
  • + accepts one level and # accepts all levels below. Topics are case sensitive.
  • Published does not mean received. Fast and safe decisions stay in the device.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.