MQTT in a Nutshell

How MQTT works

This time we will show you how the MQTT protocol works.

Publish/Subscribe Pattern

  • There are publisher and subscriber in the communication system. The publisher sends and the subscriber receives. Collectively, we call the publisher and the subscriber the clients. A client can be publisher and subscriber at the same time.
  • There is another role in the middle, who collects the messages from the publishers and dispatches them to the subscribers. Usually we call this role message broker.

Topic

For instance, if a weather service is updating the temperature of cities. The service is not aware of who’s going to receive the temperature of which city, it just marks them with a topic like “temperature/newyork” or “temperature/frankfurt” and sent them to the broker. People whos is interested in the city weather, can subscribe to one (or many) of this topics at the broker. Once a new message comes in, the broker checks the topic in it and dispatch it to the subscribers accordingly.

A topic consists of one or more levels, levels are separated by slash(/). Sometimes a topic is comparable to a category or a directory.

Wildcards (“#” and “+”) in a topic makes it easier for subscribers to subscribe to multiple topics at once. The “+” stands for one single level while the “#” stands for multiple levels. In most cases you can name a topic freely at your choice, but with one exception: Topics start with “$” are reserved for system use.

MQTT supports 1-to-many (broadcasting) messaging by its nature: A publisher publishes a message with a topic to a broker, the broker forwards this message to all clients who subscribed to this very topic.

Message

An MQTT packet consists of a fixed header, an optional variable header and an optional payload:

  • Fixed header, present in all MQTT Control Packets. It consist of the packet type, the flags and the remaining length.
  • Variable header, present in some MQTT Control Packets. The content of it varies depending on the packet type.
  • Payload, present in some MQTT Control Packets. The last part of a packet. Application related information is carried in payload.

As you can guess, the smallest MQTT packet has a size of 2 bytes.

Communication

See the figure below and image we are in a hospital:

MQTT communication, an example

Vital statistics of Stationed patients are collected by wearable devices and sent to a central location and then dispatched to concerned person (or devices). In the world of MQTT it works like this:

A sensor, here we take the thermometer as example, measures the patient’s temperature and publishes it to the MQTT broker with the topic “sensor/1/temperature”. It is clear, the role of this sensor is a publisher. People who cares about everything of this patient (like his doctor) can subscribe to “sensor/1/#”; people or device who is collecting temperature of every patients can subscribe to “sensor/+/temperature”; people who is only interested in this patient’s temperature can precisely subscribe to the topic “sensor/1/temperature”, without using any wildcard.

Things Make MQTT Special

QOS

  • 0: at most once
  • 1: at least once
  • 2: only once

Retained Message

Last Will Message

Every client can setup a last will message when it is connected to the broker. The last will message has also QoS level, retain attribute and topic just like all other messages. This message is store on the broker, when the broker detects this client lost the connection, the broker sends its last message to all the connected clients which subscribed to the topic. If this client disconnects with sending a “DISCONNECT” packet to the broker, the last will message is then discarded.

MQTT packet

The list of packet types:

List of MQTT packet types

In the next article of this series, we will demonstrate how to build a simple MQTT application and explain more about the protocol and the packet format.

to be continued…

EMQ is an open-source IoT data infrastructure software provider, delivering the world’s leading open-source MQTT message broker and stream processing database.