Everything to know about MQTT (Message Queue Telemetry Transport)
A comprehensive guide on what is MQTT
, MQTT client-broker
, publisher/subscriber architecture
, and Topics
.
Hello Jawans, 😎, today we will be learning about MQTT
, MQTT client-broker
, andpublisher/subscriber architecture
. Howzzzzz the Joshhhhhhhh …???… ⭐🤩🔥
1 — What is MQTT
MQTT (Message Queuing Telemetry Transport) according to the official website definition is a “Client Server publish/subscribe messaging transport protocol. It is lightweight, open, simple, and designed so as to be easy to implement. These characteristics make it ideal for use in many situations, including constrained environments such as for communication in Machine to Machine (M2M) and Internet of Things (IoT) contexts where a small code footprint is required and/or network bandwidth is at a premium.”
The main highlights are lightweight
because it is designed to be simple, and less resource hungry. Publish/Subscribe
, this means that the sender and receiver don’t know each other they communicate via a central broker. The publisher writes/publishes a message under a topic and subscribers will subscribe to the topic that they are interested in.
2 — How MQTT Works
To understand how MQTT works, we must first know about MQTT Client
, MQTT Broker, Publisher-subscriber, Topic, and QOS.
2.1 — What is MQTT Client
EMQX defines any application or device running the MQTT client library as an MQTT client. For example, an instant messaging app that uses MQTT is a client, various sensors that use MQTT to report real-time data are a client.
2.2 — What is MQTT Broker
MQTT Broker plays an important role in MQTT architecture. It acts as a central hub for client connection, subscription, routing, and managing the flow of messages between MQTT clients. The MQTT broker plays a critical role in enabling communication between various devices, sensors, and applications in an MQTT-based system, such as those found on the Internet of Things (IoT) and other real-time communication applications. It is highly scalable so it can handle millions of MQTT client connections.
2.3 — What is Publisher-subscriber
In the Publisher-Subscriber model, unlike the Client-Server, the client that sends the message (Publisher) is unaware of the existence of the client that receives the message (Subscriber). Clients do not connect to each other directly they connect to the Broker which is responsible for routing the messages. The MQTT Broker uses Topics for routing messages to the exact subscribed client.
2.4 — What is a Topic
MQTT Topic is a string that is used to route messages. We can create multiple hierarchies by using slash /, similar to a URL. A Publisher client can publish a message on a topic and a Broker delivers the message to all the Clients that has subscribed to that topic.
📝 If you want to learn more about MQTT Topics, Wildcards, and rules of Topic please leave a comment.
Topic Examples
sensor/1/temperature
sensor/2/humidity
software-update/android/14/
software-update/android/14/2
software-update/ios/17
software-update/ios/17/
2.5 — What is QOS
QOS or Quality of Service basically means the message delivery reliability. MQTT offers three levels of QOS.
- QOS 0: The message is delivered at most once. If the client is not available currently, it will lose this message.
- QOS 1: The message is delivered at least once.
- QOS 2: The message is delivered only once.
3 — MQTT WorkFlow
Since we are now aware of all the components of an MQTT let's see how a message is created and delivered from start to end.
- The client initiates a connection to the MQTT broker. This step also includes client authentication and session creation.
- The client either subscribes to a Topic or publishes a message to a specific topic. The Publishing client sends a message to the Broker while the Subscriber informs the Broker of its interest in any message that has been published on the topic.
- The Broker receives the published message and forwards the message to all the clients that have subscribed to that topic.
📝If you like the tutorial, please follow and clap 👏.
Please comment for any doubts or tutorials on any other topic.
Happy Coding, Let’s meet on another tutorial. Howzzzzz the Joshhhhhhhh …???… ⭐🤩🔥
📝Next Article
In our next article, we will learn how to publish and subscribe to a topic and connect to AWS Broker. We will learn how to use MQTTX to connect, publish, and subscribe topics.
Mqttx Client with AWS Mqtt Broker | by KIHSA Rai | Dec, 2023 | Medium
📝My Other Articles
Python ConfigParser: A Comprehensive Guide ⚙️ | by KIHSA Rai | Nerd For Tech | Medium
Python Profiling: Know Why and Where Your Code is Slow | by KIHSA Rai | Sep, 2023 | Medium
Happy Coding, Let’s meet on another tutorial. Howzzzzz the Joshhhhhhhh …???… ⭐🤩🔥