MQTT

MQTT(Message Queueing Telemetry Transport) is a publisher/subscriber-based communication protocol, which enables message-based transfer between applications. A central component of this transfer type is the so-called message broker, which distributes messages between the individual applications or the sender and receiver of a message. The message broker decouples the sender and receiver, so that it is not necessary for the sender and receiver to know their respective address information. During sending and receiving all communication devices contact the message broker, which handles the distribution of the messages.

MQTT 1:

Payload

The content of an MQTT message is referred to as payload. Data of any type can be transferred, e.g. text, individual numerical values or a whole information structure.

Message payload formatting

Note that the data type and the formatting of the content must be known to the sender and receiver side, particularly when binary information (alignment) or strings (with or without zero termination) are sent.

Topics

If a message broker is used that is based on the MQTT protocol, sending (publish mode) and subscribing (subscribe mode) of messages is organized with the aid of so-called topics. The message broker filters incoming messages based on these topics for each connected client. A topic may consist of several levels; the individual levels are separated by “/”.

Example: Campus / Building1 / Floor2 / Room3 / Temperature

When a publisher sends a message, it always specifies for which topic it is intended. A subscriber indicates which topic it is interested in. The message broker forwards the message accordingly.

MQTT 2:

Communication example 1 from the diagram above:

  • An application subscribes to “topic1”.
  • A controller publishes a message to “topic1”.
  • The message broker forwards the message to the application accordingly.

Communication example 2 from the diagram above:

  • A controller subscribes to “topic2”.
  • An application publishes a message to “topic2”.
  • The message broker forwards the message to the controller accordingly.

Wildcards

It is possible to use wildcards in conjunction with topics. A wildcard is used to represent part of the topic. In this case a subscriber may receive messages from several topics. A distinction is made between two types of wildcards:

  • Single-level wildcards
  • Multi-level wildcards

Example for single-level wildcard:

The + symbol describes a single-level wildcard. If it is used by the subscriber as described below, for example, corresponding messages to the topics are either received by the subscriber or not.

  • The receiver subscribes to Campus/Building1/Floor2/+/Temperature
  • The publisher sends to Campus/Building1/Floor2/Room1/Temperature - OK
  • The publisher sends to Campus/Building1/Floor2/Room2/Temperature - OK
  • The publisher sends to Campus/Building42/Floor1/Room1/Temperature - NOK
  • The publisher sends to Campus/Building1/Floor2/Room1/Fridge/Temperature - NOK

Example for multi-level wildcard:

The # symbol describes a multi-level wildcard. If it is used by the subscriber as described below, for example, corresponding messages to the topics are either received by the subscriber or not. The # symbol must always be the last symbol in a topic string.

  • The receiver subscribes to Campus/Building1/Floor2/#
  • The publisher sends to Campus/Building1/Floor2/Room1/Temperature - OK
  • The publisher sends to Campus/Building1/Floor2/Room2/Temperature - OK
  • The publisher sends to Campus/Building42/Floor1/Room1/Temperature - NOK
  • The publisher sends to Campus/Building1/Floor2/Room1/Fridge/Temperature - OK
  • The publisher sends to Campus/Building1/Floor2/Room1/Humidity - OK

QoS (Quality of Service)

QoS is an arrangement between the sender and receiver of a message with regard to guaranteeing of the message transfer. MQTT features three different levels:

  • 0 – not more than once
  • 1 – at least once
  • 2 – exactly once

Both types of communication (publish/subscribe) with the message broker must be taken into account and considered separately. The QoS level that a client uses for publishing a message is set by the respective client. When the broker forwards the message to client that has subscribed to the topic, the subscriber uses the QoS level that was specified when the subscription was established. This means that a QoS level that may have been specified as 2 by the publisher can be “overwritten” with 0 by the subscriber.

QoS-Level 0

At this QoS level the receiver does not acknowledge receipt. The message is not sent a second time.

MQTT 3:

QoS-Level 1

At this QoS level the system guarantees that the message arrives at the receiver at least once, although the message may arrive more than once. The sender stores the message internally until it has received an acknowledgement from the receiver in the form of a PUBACK message. If the PUBACK message fails to arrive within a certain time, the message is resent.

MQTT 4:

QoS-Level 2

At this QoS level the system guarantees that the message arrives at the receiver no more than once. On the MQTT side this is realized through a handshake mechanism. QoS level 2 is the safest level (from a message transfer perspective), but also the slowest. When a receiver receives a message with QoS level 2, it acknowledges the message with a PUBREC. The sender of the message remembers it internally until it has received a PUBCOMP. This additional handshake (compared with QoS 1) is important for avoiding duplicate transfer of the message. Once the sender of the message receives a PUBREC, it can discard the initial publish information, since it knows that the message was received once by the receiver. In other words, it remembers the PUBREC internally and sends a PUBREL. Once the receiver has received a PUBREL, it can discard the previously remembered states and respond with a PUBCOMP, and vice versa. Whenever a package is lost, the respective communication device is responsible for resending the last message after a certain time.

MQTT 5:

Security

When a connection to the message broker is established, it is possible to use security mechanisms such as TLS, in order to encrypt the communication link or to realize authentication between client and message broker.

Sources

For further and more detailed information about MQTT we recommend the following blog series:

HiveMq blog: http://www.hivemq.com/blog/mqtt-essentials/ (the main basis for this article)