Open In App

Introduction of Message Queue Telemetry Transport Protocol (MQTT)

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.



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.

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

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.

Disadvantages of MQTT

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 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

Article Tags :