MQTT Architecture tutorial : Message Format, types, Flow Diagram

The MQTT (Message Queuing Telemetry Transport) protocol is a lightweight messaging protocol designed for low-bandwidth, high-latency, or unreliable networks. In this guide, we’ll delve into the architecture of MQTT, exploring its message formats, types, flow diagrams and the intricate exchanges between server brokers and clients. By understanding these components, you'll gain insights into how MQTT facilitates efficient communication in real-world use cases, making it an ideal choice for IoT applications and beyond.

MQTT architecture

The figure-1 depicts MQTT architecture. As mentioned MQTT is broker based protocol. In this, end devices (i.e. Clients) communicate via a broker. The broker is a server which can be installed on any machine in the cloud. There are different types of brokers such as HiveMQ, Mosquitto etc. The single client and broker can also communicate with each other.

As MQTT runs above TCP/IP layer, it is also connection oriented protocol. The client establishes connection with the broker (i.e. Server) before the communication. MQTT is a publish-subscribe protocol. Here both client and server publish about any information (i.e. A Parameter such as temperature, humidity, event (ON/OFF) etc.) to each other using "PUBLISH" message. Any number of clients or end devices can subscribe for an event with the broker. Due to this subscription, when there is a change in any event or parameter, the broker will intimate to the subscribed clients about the change in event or parameter (i.e. Temperature, humidity, etc.).

MQTT Message types

Following table mentions MQTT protocol messages exchanged between client and Broker.

MQTT message Description with direction of flow
CONNECT Client to server (i.e. Broker), the client requests to connect to server.
CONNACK Server to client, connect acknowledgment
PUBLISH Client to server OR server to client
Publish message
PUBACK Client to server OR server to client
Publish acknowledgment
PUBREC Client to server OR server to client
Publish received
PUBREL Client to server OR server to client
Publish release
PUBCOMP Client to server OR server to client
Publish complete
SUBSCRIBE Client to server
Client subscribe request
SUBACK Server to client
Subscribe acknowledgment
UNSUBSCRIBE Client to server
Unsubscribe request
UNSUBACK Server to client
Unsubscribe acknowledgment
PINGREQ Client to server
Ping request
PINGRESP Server to client
PING RESPONSE
DISCONNECT Client to server , Client is disconnecting

MQTT Message Format

MQTT protocol message format

The figure-2 depicts MQTT message format. It consists of a fixed message header, variable header and payload. The first two bytes are used by most of the message types. Few of the message types use variable header and payload.

MQTT architecture Working using example use cases

MQTT message flow

Let us understand the working operation of MQTT protocol architecture. The figure-3 depicts MQTT message flow between client and broker. We will take two MQTT use cases to understand the working operation of MQTT architecture.

MQTT Use Case # 1

Statement: Broker wants to switch ON or OFF the light connected with remote client#1
• Initially connection is established by client#1 with a broker using CONNECT and CONNACK messages.
• Next Broker communicates with Client#1 to switch ON or OFF the light interfaced with it. The messages such as PUBLISH and PUBREC are used for it.
This use case is used to switch ON/OFF the street lights in Zigbee or LoRaWAN network. The lights are usually connected with end nodes or end devices in these wireless networks. The single Zigbee or LoRaWAN gateway controls multiple end nodes. Multiple such gateways are needed to cover the entire city.

MQTT Use Case # 2

Statement: Client#2 or client#3 wants to update temperature/humidity status to the broker based on sensors
• Client#2 and Client#3 will intimate temperature or humidity update to the broker using PUBLISH message. This information is stored in the database and will be sent to all the subscribers who have subscribed to these topics (i.e. Temperature, humidity). This information is "pushed" to all the subscribed clients of the topics.
➨If client#1 has already subscribed for subscription to topics (i.e. Temperature, humidity), it will get the information from broker using PUSH operation.
This use case is used for obtaining different types of sensing information automatically whenever there are any updates. For this purpose, different types of sensors (such as humidity sensor, temperature sensor, etc.) are interfaced with end nodes. These end nodes publish information (of any event updates) to the broker. The broker intimates changes to all the subscribed clients. The communication between gateway

There are two dominant data exchange protocol architectures viz. broker based and bus based. In this MQTT tutorial we have studied broker based MQTT protocol architecture. Wireless IoT technologies such as zigbee, LoRaWAN uses MQTT for communication between clients and router. Protocols such as AMPQ, CoAP and JMS also use broker based architecture. Protocols such as DDS, REST and XMPP use bus based architecture. Refer Bus based vs Broker based architecture >> for more information.

Summary

In conclusion, understanding the architecture, message format, and flow of MQTT is essential for leveraging its full potential in various applications, especially in IoT environments.

Difference between MQTT and other protocols

Zigbee and LoRaWAN Related Links

IoT Wireless Technologies

WLAN    THREAD    EnOcean    LoRa    SIGFOX    WHDI   
Zigbee   6LoWPAN   Zigbee RF4CE   Z-Wave   NFC   RFID   INSTEON  

What is difference between

RF Wireless Tutorials