MQTT is a lightweight, open-source communication protocol designed for constrained devices. This article explains MQTT in a simple way.
MQTT stands for Message Queue Telemetry Transport. MQTT gained more popularity with the explosive growth of IoT devices. Its low-bandwidth usage makes it a perfect protocol for the Internet of Things applications.
Background
The first version of MQTT was developed in 1999. It was developed to monitor the oil pipeline through the desert. The main goal was to build low bandwidth, lightweight protocol. That is still what MQTT is known for, but it found its true soulmate, the Internet of Things.
Post Office Analogy
No matter what part of the world you live in, you might have been to or at least heard of the post office. The post office is a central body that sends and receives letters. People can send or receive a letter. They have to place the letter in an envelope, address it, and submit it to the post office. The post office sends the letter to the intended receiver. All letters go through the post office. So, it can be called a broker that handles all the clients.
Concepts
To understand the MQTT protocol, it is important to know about some terminologies.
Broker
The broker is a central server that does these things
- receive all the messages
- filter messages
- publish the message to subscribed clients
Topic
The topic is nothing but a string that the broker uses to filter the messages for each connected client. Example topic looks like this: "home/bedroom/lamp". Just some words separated by forward slashes "/"
Subscribe/Publish
MQTT works on the subscribe/publish mechanism. Client 1 publishes on the topic and Client 2 subscribes to the same topic. Now, Client 2 receives the message published on the topic by Client 1.
Message
The message is information that you want to send and receive. It can also be a command.
How it Works
Now that all the concepts are clear, the working of MQTT is pretty easy to understand.
In the above case, 3 clients are registered with the MQTT broker. The first client is a smartphone application and the second and third are two smart lamps respectively.
- Assuming the living room lamp is in OFF state and the bedroom lamp is in ON state
- The living room lamp is subscribed to the topic "home/livingroom/lamp"
- The bedroom lamp is subscribed to the topic "home/bedroom/lamp"
- The registered client, the Smartphone application, sends a publish message to topic "home/livingroom/lamp" with the message "ON" and to topic "home/bedroom/lamp" with the message "OFF".
- As the lamps are subscribed to a relevant topic, the broker routes the message to the respective lamps and the command ON/OFF is used to control the lamp.
- Now, the living room lamp is turned ON, and the bedroom lamp is turned OFF.
Applications of MQTT protocol in IoT
The example explained above is one of the applications of the MQTT protocol in smart homes. As it uses lesser power and needs low bandwidth, it is perfect for all IoT applications that are power and performance constraint. It is usually used as a messenger. So, it is not very suitable if you plan to transfer a huge amount of data (For example, upload and download of video files).
Upcoming articles on MQTT
- Deep dive into MQTT - more concepts like Quality of Service (QoS), Keepalive, Last will, Persistence, and Retained messages.
- Setting up an MQTT broker in your development environment.
- Running MQTT broker in the cloud.