Share
MQTT

MQTT: Communication Standard for the IoT (Image Source: spiceworks)

What is MQTT?

MQTT, Message Queuing Telemetry Transport, is a communication standard often used in scalable and responsive IoT systems. It is a light weight IoT messaging protocol that ensures reliable and secure communication over unreliable channels. It is an event-driven protocol that uses the Publisher/Subscriber model to exchange data over the internet in near real time, with delivery guarantees.

The fact that it is an open-source protocol, scalable up to millions of devices, easy to implement, secure and is not affected by tight bandwidth and power constraints, makes it the obvious choice for IoT applications.

Origin

MQTT was originally developed by IBM engineers to monitor sensor data from oil pipelines using satellite link. While doing so, they outlined a set of requirements which served as the basis for all IoT communication. These requirements are as follows:

  1. Simple Implementation
  2. Quality of Service Delivery
  3. Lightweight and Bandwidth Efficiency
  4. Independent of Type of Data
  5. Continuous Session Awareness

How Does MQTT Work?

The key feature of MQTT Publisher-Subscriber model of exchanging information.

In the case of classic internet, protocols like http implement a Client-Server model or a Request-Response model, in which sender and receiver are directly connected. Furthermore, a client must generate a request for the server to get the required data.

MQTT Work

Client-Server Model (Image Source: hithub)

However, in MQTT Publisher-Subscriber Model, we have:

  1. An MQTT Client: Any device in the network that utilizes MQTT protocol is MQTT client. The client that generates and sends data is referred to as a publisher. Whereas any device that receives and utilizes that data is referred to as a subscriber. A publisher and subscriber don’t need to be in direct contact with each other. For instance, a publishing client can be a temperature sensor at a certain location in a control system and the subscriber client can be a processing node attached to a circuit breaker that needs the temperature information to turn off/on a heating element.
  2. An MQTT Broker: The second component is a central node or backend system, referred to as broker. It receives messages from publishers, sorts them according to the ‘topic’ and then routes them to the interested subscribers.

MQTT Pub-Sub Model

 

MQTT Pub-Sub Model (Image Source: emqx)

Like most of the internet protocols today, MQTT is built on the TCP/IP, to reap the already established benefits of TCP such as guaranteed and reliable packet delivery over the internet. Furthermore, we also have added security in the form of TLS, or Transport Layer Security, above TCP layer.

MQTT TCP Stack

MQTT TCP Stack (Image Source: Peter R Egli)

What are the Advantages of MQTT?

IoT devices are often resource constrained. Many IoT devices have little processing power, very little memory, limited bandwidth, and constrained battery backup. On top of that, they operate in circumstances prone to frequent disconnections and must perform unattended network operations.

MQTT proves valuable in such scenarios for several special features, some of which are as follows:

  • Decoupling
    • Space Decoupling: The publisher and subscriber don’t need to know each other’s network location and exchange information independently.
    • Time Decoupling: The publisher and subscriber don’t need to be online at the same time to establish a connection and hence are unaffected by connectivity outage.
    • Synchronization Decoupling: The publisher and receiver can both exchange information at their own speeds, without interrupting each other or the need to get synchronized.

 

  • Quality of Service Levels

MQTT defines a certain level of Quality of Service to make sure that the information is exchanged between clients in a reliable manner.

    • QoS0: Guarantees delivery of messages at most once. No ACK is sent. Also known as Fire and Forget. Message may get lost in network. QoS0 has the lowest bandwidth overhead.
    • QoS1: Guarantees delivery of messages at least once. ACK is sent after delivery. Therefore, multiple SENDs are possible. This is the default QoS for MQTT. QoS1 a compromise between bandwidth and performance.
    • QoS2: Guarantees delivery of message exactly once. Message is followed by an ACK, which is responded by subsequent messages of ACK_Response and then SEND_Complete. QoS2 is the most reliable but bandwidth inefficient as compared to QoS1.

 

  • Security

MQTT makes it easy to encrypt messages using TLS and authenticate clients using modern authentication protocols, such as OAuth, making it very secure.

  • Last Will and Testament

Last Will and Testament, LWT, is a unique feature of MQTT that is used for error detection. It enables an MQTT client to notify other clients if they disconnect unexpectedly.
When a client connects to an MQTT network, it defines an LWT message that will be sent by the broker to all the subscribers on behalf of the publisher whenever it gets offline without informing the broker. Normally, this scenario happens when a previously defined ‘Keep Alive’ time interval has passed without any heartbeat from the client.
LWT is a key component for reliability and robustness of MQTT network.

  • Persistent Sessions

It is another important feature for maintaining seamless communication in IoT systems where devices frequently connect and disconnect. Persistent sessions mean that the broker retains a client’s session state even after the client disconnects.

When the client reconnects, it resumes its session without needing to resubscribe to topics or retrieve missed messages manually, ensuring reliable and continuous message delivery despite intermittent connectivity.

Where is MQTT Used?

MQTT’s efficiency and flexibility have made it a popular choice in many real-world IoT applications. One example is in home automation systems, where MQTT enables communication between smart home devices, allowing for seamless control and monitoring.

Another application is in agriculture, where MQTT is used to collect and transmit data from sensors monitoring soil moisture, temperature, and other environmental factors to optimize crop growth.

Applications of MQTT

Applications of MQTT (Image Source: hornerautomation)

MQTT is also used in transportation, where it enables communication between vehicles and traffic management systems to improve safety and efficiency on the roads.

In the healthcare field, MQTT plays a crucial role in transmitting real-time data from medical devices like heart monitors and blood pressure monitors. This enables healthcare providers to remotely monitor and care for patients.

Similarly, for Industrial applications, MQTT provides a very reliable and structured approach to manage communication between key sensor nodes, processing elements, actuators and control nodes for industries like manufacturing, automation, automotive, oil and gas and many more.

Where is MQTT Not Suitable?

MQTT is not a perfect match to every IoT application. MQTT works on TCP/IP and if the application relies on underlying network with high latency, such as 5G NTN with high roundtrip time of up to 40 seconds, and even LTE-M and NB-IoT that can have latencies of up to 10 seconds, it can cause disconnects in TCP/IP.

Although, MQTT handles reattempts to ensure the data gets delivered, but that will make it inefficient for applications with ultra-low power and realtime data transmission requirements.

In such cases, alternatives such as CoAP (Constrained Application Protocol) seems a better choice in as it uses UDP instead of TCP/IP.

MQTT shouldn’t be a developer’s choice where high datarates and low-latency realtime communication is needed, or even for ultra-low power applications. It would also not be a developer’s choice where advanced routing or message queuing features are required.

 

Summary

MQTT’s characteristics made it an ideal choice for many IoT applications:

𝗟𝗶𝗴𝗵𝘁𝘄𝗲𝗶𝗴𝗵𝘁: Suitable for devices with limited processing power and memory.
𝗟𝗼𝘄 𝗣𝗼𝘄𝗲𝗿: Perfect for battery-operated devices that need to conserve energy.
𝗟𝗼𝘄 𝗕𝗮𝗻𝗱𝘄𝗶𝗱𝘁𝗵: Minimal bandwidth usage was critical for devices communicating over wireless or constrained networks.
𝗦𝗰𝗮𝗹𝗮𝗯𝗹𝗲: Able to handle thousands of devices publishing and subscribing to various topics.

With the rise of IoT, MQTT was recognized for its suitability in connecting a wide range of devices, from industrial machines to home automation systems.
The protocol became an OASIS standard in 2013, solidifying its place in the industry.
Its 𝗼𝗽𝗲𝗻 𝘀𝘁𝗮𝗻𝗱𝗮𝗿𝗱 nature encouraged adoption and contributed to the development of a 𝗹𝗮𝗿𝗴𝗲 𝗲𝗰𝗼𝘀𝘆𝘀𝘁𝗲𝗺 of tools and libraries.

MQTT Merits

MQTT Merits (Image Source: spiceworks)

Still, there are no silver bullets. MQTT is not a one-size-fits-all solution. Choosing the right protocol depends on the specific constraints of the application.

References:

  1. mqtt.org
  2. hivemq.com
  3. emqx.com
  4. spiceworks.com
  5. hornerautomation.eu
  6. hithub.io
Was this article of help to you?
Subscribe to our newsletter. We write about developing embedded and electronic systems.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe Our Newsletter