MQTT (Message Queue Telemetry Transport) is an open messaging protocol for machine-to-machine communication (M2M) that enables the transmission of telemetry data in the form of messages between devices, despite high latency or constrained networks. Corresponding devices range from sensors and actuators, mobile phones, embedded systems in vehicles or laptops to fully developed computers. [Wikipedia]

The information about the specification listed below comes, among other sources, from the current MQTT specification of IBM.

OSI Within the OSI model, MQTT is positioned as follows: The MQTT specification distinguishes between TCP/IP-based and non-TCP/IP networks. By default, MQTT uses the TCP protocol/connection (port 1883 and 8883 for an SSL connection). In MQTT-SN (MQTT for Sensor Networks), the UDP protocol is used instead of TCP.

MQTT packet structure The fixed header is always present, while the ‘variable header’ and ‘payload’ do not always occur.

Fixed header The ‘fixed’ header of an MQTT message has a length of two bytes and is divided as follows:

Listing of the ‘Message Types’ - byte 1, bits 7-4: For a detailed description, I refer to the specification chapter 3.

Listing of byte 1, bits 3-1:

RETAIN When a subscriber registers for a topic, they only receive data when the publisher makes new data available. If the data are, for example, transmitted once per day, the subscriber will have to wait until the new data set arrives. To counteract this situation, the publisher can ensure that new subscribers receive the last data set (although it may be outdated). For this, the RETAIN flag must be set to 1.

Quality of Service (QoS) QoS or service quality denotes the quality of a communication service from the users’ perspective. That is, how well the quality of the service matches their requirements. Formally, QoS is a set of quality requirements on the collective behavior or interaction of multiple objects.

Standard IEEE 802.1p

  • A user wants to be reliably connected to the desired target and reliably disconnected after the communication ends.
  • The connection setup should occur promptly.
  • Problems during connection setup (e.g. target participant not reachable) should be communicated to the user as quickly as possible.
  • A communication connection should remain stable.
  • The communication participants want to understand each other.
  • The information should be transmitted completely and without errors.
  • No information from other communication participants and no interference should be transmitted.
  • The communication should take place as faithfully as possible.
  • There should be no long waiting times during communication. Billing of the communication should correspond to the correct time and data volume. [Wikipedia]

In the context of MQTT, the following three QoS levels are distinguished from each other:

Below is the representation of the QoS variants as a sequence diagram for better understanding:

Duplicate delivery (DUP) This flag is always set when the client or server wants to resend the following commands:

  • PUBLISH
  • PUBREL
  • SUBSCRIBE
  • UNSUBSCRIBE

DUP can be set for QoS > 0.

Variable header In addition to the fixed header, MQTT messages can include a variable header.

Message identifiers

The variable header of the following ‘Message Types’ is described by ‘Message identifiers’:

  • PUBLISH
  • PUBACK
  • PUBREC
  • PUBREL
  • PUBCOMP
  • SUBSCRIBE
  • SUBACK
  • UNSUBSCRIBE
  • UNSUBACK

Payload The following MQTT commands have a payload: