Skip to content
Related Articles

Related Articles

Improve Article
Introduction of Message Queue Telemetry Transport Protocol (MQTT)
  • Last Updated : 30 Jul, 2020

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 –


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 :


    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:


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

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
Recommended Articles
Page :