Open In App

Introduction of Message Queue Telemetry Transport Protocol (MQTT)

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Message Queuing Telemetry Transport, or MQTT, is a communications protocol designed for Internet of Things devices with extremely high latency and restricted low bandwidth. Message Queuing Telemetry Transport is a perfect protocol for machine-to-machine (M2M) communication since it is designed specifically for low-bandwidth, high-latency settings.

What is Message Queue Telemetry Transport Protocol(MQTT)?

MQTT is a simple, lightweight messaging protocol used to establish communication between multiple devices. It is a TCP-based protocol relying on the publish-subscribe model. This communication protocol is suitable for transmitting data between resource-constrained devices having low bandwidth and low power requirements. Hence this messaging protocol is widely used for communication in the IoT Framework.

Publish-Subscribe Model

This model involves multiple clients interacting with each other, without having any direct connection established between them. All clients communicate with other clients only via a third party known as a Broker. 

MQTT Client and Broker

Clients publish messages on different topics to brokers. The broker is the central server that receives these messages and filters them based on their topics. It then sends these messages to respective clients that have subscribed to those different topics. The heart of any publish/subscribe protocol is the MQTT broker. A broker can handle up to thousands of concurrently connected MQTT customers, depending on how it is implemented. All communications must be received by the broker, who will then sort them, ascertain who subscribed to each one, and deliver the messages to the clients who have subscribed. All persistent customers’ sessions, including missed messages and subscriptions, are likewise kept by the Broker.

Message Queue Telemetry Transport Protocol

Publish-Subscribe Model 

Hence client that has subscribed to a specific topic receives all messages published on that topic. 

Here the broker is central hub that receives messages, filters them, and distributes them to appropriate clients, such that both message publishers, as well as subscribers, are clients.

Working of MQTT

MQTT’s publish/subscribe (pub/sub) communication style, which aims to maximise available bandwidth, is an alternative to conventional client-server architecture that communicates directly with an endpoint. In contrast, the client who transmits the message (the publisher) and the client or clients who receive it (the subscribers) are not connected in the pub/sub paradigm. Third parties—the brokers—manage the relationships between the publishers and subscribers because they don’t communicate with one another directly.

Publishers and subscribers, which denote whether a client is publishing messages or has subscribed to receive messages, are examples of MQTT clients. The same MQTT client can be used to accomplish these two features. A publish occurs when a client or device want to submit data to a server or broker.

The term “subscribe” refers to the reversal of the procedure. Several clients can connect to a broker under the pub/sub paradigm and subscribe to subjects that interest them.

Introduction-of-Message-Queue-Telemetry-Transport-Protocol-(MQTT)

MQTT

When a broker and a subscribing client lose contact, the broker will store messages in a buffer and send them to the subscriber whenever the broker is back up and running. The broker has the right to cut off communication with subscribers and send them a cached message containing publisher instructions if the publishing client abruptly disconnects from the broker.

“Publishers send the messages, subscribers receive the messages they are interested in, and brokers pass the messages from the publishers to the subscribers,” reads an IBM write-up describing the pub/sub paradigm. MQTT clients, such as publishers and subscribers, can only speak with MQTT brokers. Any device or programme that runs a MQTT library can be a MQTT client, ranging from microcontrollers like the Arduino to entire application servers housed in the cloud.

Characterstics of MQTT

  • Lightweight: MQTT is designed to be lightweight, making it suitable for use in aid-restrained environments inclusive of embedded systems and low-strength devices. The protocol minimizes bandwidth and processing overhead, enabling green communication even on restricted networks.
  • Publish-Subscribe Model: In the publish-subscribe version, clients (publishers) send messages to subjects, and different clients (subscribers) acquire messages from subjects of interest. This decoupling of producers and purchasers permits for flexible and dynamic conversation styles.
  • Quality of Service (QoS) Levels: MQTT supports exclusive stages of message delivery warranty, referred to as Quality of Service (QoS). QoS levels range from 0 to 2, providing various stages of reliability and message transport guarantees, relying at the utility necessities.
  • Retained Messages: MQTT lets in agents to store retained messages on topics, making sure that new subscribers acquire the maximum latest message posted on a subject right now after subscribing. This characteristic is beneficial for fame updates and configuration settings.
  • Last Will and Testament (LWT): MQTT clients can specify a Last Will and Testament message to be posted by way of the broker in the occasion of an sudden consumer disconnect. This function affords a mechanism for detecting patron failures and dealing with them gracefully.
  • Security: MQTT helps various protection mechanisms, consisting of Transport Layer Security (TLS) encryption and authentication mechanisms which include username/password and consumer certificates. These capabilities make certain the confidentiality, integrity, and authenticity of messages exchanged over MQTT connections.

Advantages of MQTT

This model is not restricted to one-to-one communication between clients. Although the publisher client sends a single message on specific topic, broker sends multiple messages to all different clients subscribed to that topic. Similarly, messages sent by multiple such publisher clients on multiple different topics will be sent to all multiple clients subscribed to those topics. Hence one-to-many, many-to-one, as well as many-to-many communication is possible using this model. Also, clients can publish data and at the same time receive data due to this two-way communication protocol. Hence MQTT is considered to be bi-directional protocol. The default unencrypted MQTT port used for data transmission is 1883. The encrypted port for secure transmission is 8883.

  • Lightweight protocol that is quick to create and allows for efficient data transport
  • Minimal data packet usage, resulting in low network usage
  • Effective data dispersion
  • The effective use of remote sensing and control
  • Prompt and effective message delivery
  • Minimises power consumption, which is beneficial for the linked devices, and maximises network capacity.
  • Data transmission is quick, efficient, and lightweight because MQTT messages have small code footprint. These control messages have a fixed header of size 2 bytes and payload message up to size 256 megabytes. 

Disadvantages of MQTT

  • When compared to Constrained Application Protocol (CoAP), MQTT has slower send cycles.
  • Resource discovery in MQTT is based on flexible topic subscription, while resource discovery in CoAP is based on a reliable system.
  • MQTT lacks encryption. Rather, security encryption is accomplished by TLS/SSL (Transport Layer Security/Secure Sockets Layer).
  • Building an internationally scalable MQTT network is challenging.

What is Topic?

In MQTT, topic is UTF-8 string that the broker uses to filter messages for each individual connected client. Each topic consists of one or more different topic levels. Each topic level is separated by forward slash also called topic level separator. Both topics and levels are case-sensitive.

Example of topic –  

home/kitchen/table 

Here, “home”, “kitchen” and “table” are different levels of topic.

Wildcard is an additional feature used in MQTT to make topics and their levels more flexible and user-friendly. 

MQTT Topics include two types of wildcards: 

1. Single Level: “+” 
Single-level wildcard represented by “+” symbol can replace single level in topic. 

Example – 
If the client wants information about all tables present inside the house, it will subscribe to the topic : 

home/+/table 

Hence any information published related to tables, inside the kitchen, living room, bedroom, etc, can be obtained on this topic. 

Figure – Single-Level Topics in MQTT 

2. Multi-Level: “#” 
Multi-level wildcard represented by “#” symbol can replace multiple levels in topic.

Example – 
If a client wants information about all objects present inside the kitchen, living room, bedroom, or any other room on ground floor, it will subscribe to topic: 

home/groundfloor/# 

Hence any information published on topics related to kitchen items, bedroom items, living room items can be obtained on this topic. Information up to multiple levels can be obtained in this case.

The following program illustrates how MQTT can be implemented in javascript. 

Javascript




//JavaScript Program for publish-subscribe model
/* jshint esversion : 6 */
"use strict";
 
//Importing MQTT
var mqtt = require('mqtt');
 
//Creating an instance of the client
var client = mqtt.connect({clientId: "001"});
 
//Definiting constants
var topic = "home/kitchen/table";
var message = "Table inside the kitchen";
var options = {retain: false, qos: 1};
 
//On successful connection
client.on('connect', function () {
    console.log(" After successful connection: ", client.connected);
    //If client is connected, then publish on the topic
    if (client.connected) {
        console.log(" Publishing on topic: ", topic);
        client.publish(topic, message, options);
    }
});
 
//On connectivity error
client.on('error', function (error) {
    console.log(" Connection error: ", error);
});
 
//On receiving message
client.on('message', function (topic, message) {
    console.log(" Received message: ", message.toString(), "on topic: ", topic);
    client.end();
});
 
function init() {
    //Subscribing to the topic
    console.log("\n Subscribing to topic");
    client.subscribe(topic, {qos: 1});
}
 
//Start of the program
init();


Frequently Asked Question on MQTT – FAQs

What is MQTT?

MQTT is a simple, lightweight messaging protocol used to establish communication between multiple devices. It is TCP-based protocol relying on the publish-subscribe model.

What is MQTT broker?

The heart of any publish/subscribe protocol is the MQTT broker. A broker can handle up to thousands of concurrently connected MQTT customers, depending on how it is implemented. All communications must be received by the broker, who will then sort them, ascertain who subscribed to each one, and deliver the messages to the clients who have subscribed. All persistent customers’ sessions, including missed messages and subscriptions, are likewise kept by the Broker.

What is a MQTT payload?

Using a broker, MQTT allows messages to be shared with other hardware or applications. Each communication includes a topic that determines how the Broker can proceed with processing it. Every communication also has a message content, or “payload,” in addition.

What is MQ Telemetry Transport in IoT?

Resource-constrained Internet of Things (IoT) devices can publish or transfer information on a particular subject to a server that serves as a MQTT message broker by using MQ Telemetry Transport. The information is thereafter sent by the broker to the clients who have already subscribed to the client’s topic.

Is there any benefits of using MQTT?

  • lightweight protocol that is quick to create and allows for efficient data transport
  • minimal data packet usage, resulting in low network usage
  • effective data dispersion
  • the effective use of remote sensing and control


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads