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(); |
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.
Contact Us