BACK TO BLOG

MQTT Protocol: A Comprehensive Guide to IoT Communication

Published Date

March 15, 2024

Read

9 minutes

Written By

ACL Digital

After years of extensive development, the MQTT protocol emerged as a solution to critical challenges in IoT device communication. It has since then become a preferred communication protocol for the IoT industry due to its unique features like lightweight design, extensive support for massive connections and secure bidirectional communication. 

In the State of IoT—Spring 2023 report by IoT Analytics, "It is anticipated that the number of connected IoT devices will increase by 18%, reaching a total of 16.7 billion in the year 2023.”

How MQTT Communication Works

Several components collaborate to enable the functioning of the MQTT protocol. These are, 

  • Publisher publishes a message to a specific "Topic," such as "SensorData." The message could be data like temperature readings
  • Topic has this unique identifier as publisher sends a message to the topic and this further passes it on to the Subscriber
  • MQTT Broker acts as a middleman. It receives the message from the publisher and checks the topic it is published to "SensorData"
  • Subscriber has subscribed to the "SensorData" topic. As soon as the publisher publishes a message to this topic, the broker forwards the message to the subscriber
  • Retained Message every client will automatically receive on subscribing

Let's examine the diagram below to understand the workings of MQTT communication.

MQTT Architecture

Advanced-level MQTT Broker Features

Last Will and Testament (LWT)

LWT is a feature that allows a client to specify a message to be sent by the broker in case the client disconnects unexpectedly. This is useful for detecting client disconnections and reacting accordingly.

Retained Messages

MQTT brokers can store the last message with the "retain" flag set for specific topics. When a new client subscribes to a topic, it receives the most recent retained message immediately upon subscription.

Session Persistence

MQTT supports both clean and persistent sessions. In a clean session, the broker discards all subscriptions and messages for a client when it disconnects. In a persistent session, the broker saves the client's subscriptions and undelivered messages, ensuring continuity across disconnections.

Message Persistence

Some MQTT brokers provide options for message persistence, allowing messages to be stored in a database or disk for reliability and durability, even in the event of broker failures.

Bridge Connections

MQTT brokers can be connected to each other via bridge connections, allowing messages to be forwarded between different MQTT broker instances. This is useful for building larger-scale and distributed MQTT networks.
 

Unique Capabilities of MQTT for IoT

MQTT minimizes protocol overhead with a 2-byte header, ensuring reliable operation in bandwidth-limited networks and supports smooth performance on Low-capacity Devices. In this context, EMQX emerges as a standout MQTT broker, showcasing unparalleled scalability by supporting a Massive Number of Connected Devices. The recent achievement of EMQX 5.0, managing 100 million MQTT connections and processing 1 million messages per second through a 23-node cluster, underscores its global leadership in the MQTT space. 

The MQTT protocol provides 3 levels of Quality of Service for messaging, ensuring Reliable Message Delivery in different network environments.

QoS Publisher Subscriber
0 Ensure message is sent only once The message might or might not receive
1 Send a message at least once until acknowledgment or command to end transmission is received Expect to receive the message at least once, with the possibility of receiving it multiple times
2 Send the message only once Receive the message only once

 

Reviewing the data in this table will enhance your comprehension of each QoS level for both publishers and subscribers. Let's look into the specifics of each QoS level and examine their practical applications.

QoS 0:

QoS 0 in MQTT provides a "fire and forget" delivery approach, where the acknowledgment or assurance of message delivery is not expected. In this scenario, the recipient doesn't acknowledge and the sender does not store or retransmit the message. QoS 0 operates like TCP, ensuring messages are sent without additional follow-up or confirmation.

QoS 0

QoS 1:

The aim of QoS 1 is to ensure message delivery at least once to the receiver. The sender keeps a copy until receiving a PUBACK packet, confirming successful receipt. If not received promptly, the sender re-transmits for delivery assurance.

QoS 1

QoS 2: 

QoS 2 offers the highest level of service in MQTT, ensuring that each message is delivered exactly once to the intended recipients. To achieve this, QoS 2 involves a four-part handshake between the sender and receiver.

QoS 2

MQTT facilitates bi-directional messaging between devices and the cloud by leveraging the publish-subscribe model. Thus, publishers and subscribers are not required to establish a direct connection or be online simultaneously; the message server takes charge of routing and distributing all messages. Security stands as the foundation of all IoT applications. MQTT ensures Secure Bi-Directional Communication through TLS/SSL. Additionally, the MQTT protocol provides client ID, username, and password, empowering users to enforce authentication and authorization at the application layer.

MQTT addresses network instability using a Keep Alive feature, ensuring the connection remains uninterrupted during extended periods of message inactivity. If a disconnection occurs, the client quickly detects and reconnects. Additionally, MQTT has Last Will, letting the server send a message to a specific topic if the client unexpectedly goes offline. Some MQTT brokers, like EMQX, provide extras like online and offline event notifications, streamlining how backend services handle client events.

Use Cases for MQTT in IoT

IBM initially designed MQTT to help oil and gas companies to send and get data from sensors located remotely. As time passed, IBM set up specifications for MQTT, and different industries started using MQTT for their own IoT needs. Let's check out some industries using MQTT for their IoT work.
 

Smart Homes and Wearable Devices

MQTT is crucial for communication among various consumer IoT devices, encompassing smart home gadgets (thermostats, appliances, security systems) and wearable tech (smartwatches, fitness trackers, health monitoring tools). Its widespread adoption and performance benefits make it ideal for maximizing battery life and minimizing bandwidth consumption in consumer IoT applications. This has led to the development of an ecosystem of IoT frameworks like Node-Red and Home Assistant, integrating with MQTT to simplify device connections and enable users to create automation and alerts tailored to their needs.

Manufacturing

Modern manufacturing increasingly relies on data for cost-effectiveness and responsiveness to market demands. MQTT plays a pivotal role by facilitating the movement of data from the factory floor to the cloud, addressing issues such as data silos and proprietary vendor-specific protocols. Common use cases include using telemetry data to enhance product reliability through automated testing during assembly and increasing assembly line uptime via predictive maintenance.

Automotive

Automobile manufacturers leverage MQTT to enhance products and create new revenue streams. For instance, BMW built a car-sharing service managing over 14,000 vehicles across 18 cities on top of MQTT. Common applications include predictive maintenance to extend vehicle life, data transmission for driver assistance features, and communication related to autonomous driving. Government adoption of MQTT in vehicle-to-vehicle (V2V) and vehicle-to-everything (V2X) communication is estimated to reduce traffic accidents by 13% annually.

Logistics

MQTT is ideal for real-time monitoring of assets worldwide, even in the absence of internet connectivity during transit. It ensures the delivery of data once connectivity is restored, proving crucial in complex supply chains for visibility and rapid issue mitigation.

Smart Cities

Cities adopt IoT technology for efficiency and sustainability, with MQTT serving as a common communication protocol for diverse devices. The IoT market for smart cities is projected to reach around $260 billion by 2025, encompassing sensors, cameras, and devices for tasks like tracking traffic flow, energy usage, and water management.

Agriculture

Agricultural businesses adopt IoT solutions for managing crop farming and livestock. Sensors collect data on soil conditions, temperature, and moisture for forecasting yields. MQTT facilitates the movement of data from field sensors to the cloud, enabling applications such as health monitoring for livestock and tracking their movement through pastures and fields.

Mobile Application Development

MQTT is widely used in mobile apps, as seen in Facebook Messenger and Instagram. Chosen over HTTP for its suitability in IoT-like workloads, MQTT's pub/sub architecture aligns with group chats in Messenger and facilitates push notifications in Instagram. Ideal for mobile apps requiring frequent network communication, MQTT's ability to establish persistent connections allows devices to receive new messages without excessive bandwidth consumption or battery drain.

MQTT Protocol serves as a pivotal solution in IoT communication. Its lightweight design, robust features, and seamless operation create opportunities for efficient data exchange, driving the advancement of smart solutions and enhancing connectivity in our increasingly interconnected world. With its unique capabilities and widespread adoption, MQTT continues to revolutionize IoT applications, offering endless possibilities for industry advancements and technological evolution. 

For the latest updates or additional information, feel free to reach out to ACL Digital. Drop us a message at business@acldigital.com.
 

About the Author

ACL Digital

Related Posts

Transforming Energy Management with IoT

Published Date: November 27, 2024

By: ACL Digital

Protect and Validate Essential JTAG Practices for Electronics

Published Date: August 22, 2024

By: ACL Digital