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.
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.
- 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.
- 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.
- 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.
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:
- Single Level: “+”
Single-level wildcard represented by “+” symbol can replace single level in topic.
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
- Multi-Level: “#”
Multi-level wildcard represented by “#” symbol can replace multiple levels in topic.
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.
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.