MQTT in a Nutshell

— Part Two, How It Works

How MQTT works

Unlike the traditional client/server protocol, MQTT is not end to end. MQTT is a publish/subscribe message exchanging protocol. Also, unlike message queue, MQTT doesn’t maintain any queue. It uses topics to route messages.

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

Publish/Subscribe Pattern

MQTT is a publish and subscribe messaging exchange protocol, this means:

  • 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

Another important concept of MQTT is topic, each message has a topic, it decides which message is going to be received by whom. Topic is the routing mechanism for MQTT.

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

A Message is the information we want exchange with other parties within a communication system. Exchanging message is the goal of MQTT. MQTT uses control packet to deliver message and maintain the connection.

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

Now we have the communication roles, the way to route messages and an agreed message format, we can start building a communication system with these basic elements.

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

MQTT implements a simple but effective 3-level QoS system:

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

Retained Message

MQTT messages can be retained by the broker if its retain flag is set. When a client connects to a broker and subscribes to a topic, and the broker has a retained message of that particular topic, the client will receive is at once. For each topic there is at most one retained message.

Last Will Message

MQTT is often used in an unreliable network environment, a client could lost its connection without proper disconnection (DISCONNECT message not sent). In this situation, a last will message can do what the client ought to do.

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

MQTT defines 14 message types. They are used to connect/disconnect, to publish messages, to subscribe to topics, to maintain the connection or to ensure the QoS.

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…