Introduction of Message Queue Telemetry Transport Protocol (MQTT)

MQTT is simple, lightweight messaging protocol used to establish communication between multiple devices. It is TCP-based protocol relying on 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 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 third party known as Broker.

MQTT Client and Broker :
Clients publish messages on different topics to broker. The broker is 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.

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


Figure – Publish-Subscribe Model




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.

Advantages :

  1. Easy Scalability –
    This model is not restricted to one-to-one communication between clients. Although publisher client sends 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.

  2. Eliminates insecure connections –
    In a complex system where multiple devices are connected with each other, each device not only has to manage its connections with other devices but also has to ensure that these connections are secure. But in publish-subscribe model, broker becomes central server managing all security aspects. It is responsible for authentication and authorization of all connected clients.

  3. Lightweight Communication –
    Data transmission is quick, efficient, and lightweight because MQTT messages have small code footprint. These control messages have fixed header of size 2 bytes and payload message up to size 256 megabytes.

Topics :
In MQTT, topic is UTF-8 string that 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 client wants information about all tables present inside house, it will subscribe to topic :

    home/+/table 

    Hence any information published related to tables, inside 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 client wants information about all objects present inside 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

filter_none

edit
close

play_arrow

link
brightness_4
code

//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() {
    //Suscribing to the topic
    console.log("\n Subscribing to topic");
    client.subscribe(topic, {qos: 1});
}
  
//Start of the program
init();

chevron_right


Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.