In this article, I will explain the fundamentals of MQTT, how it works and its advantages.
If you already know how it works and want to jump to learning paho-mqtt(Python MQTT Client library) go to Getting Started with MQTT
1. What is MQTT?
In short, MQTT(Message Queuing Telemetry Transport) is a light-weight messaging protocol used in IoT for sending messages to & from devices. It runs over TCP/IP networks. There are many other messaging protocols that can be used for IoT but MQTT is fast becoming the most popular protocol because of its simplicity & ease-of-use. The latest version of MQTT is v5 but v3.1.1 & v3.1 are still the most commonly used.
2. How does MQTT Work?
MQTT works through what is called a publish/subscribe model. An MQTT setup has 2 main components:
Broker: A broker is a central hub or server that is responsible for all the connections between clients & storing of all the messages.
Client: A client is a device that connects to the broker and can publish & receive messages.
The MQTT broker is like a radio wave and the MQTT clients are the devices that broadcast & receive messages through that wave.
3. So how does the messaging actually work?
The clients that are connected to the broker can communicate with each other through topics.
A topic is like a radio frequency. When you broadcast a message to a certain frequency, anyone tuned-in to that frequency will receive that message. In MQTT, broadcasting is “Publishing” & tuning-in is “Subscribing”. You can use words as topics. For example:TemperatureSensor, Kitchen/Lights
If I have 2 clients connected to a broker and if I want to send messages between the clients, I have to publish the message to a topic name say “Lights” from one client. The other client that is supposed to receive the message has to be subscribed to that same topic “Lights”.
4. Advantages of MQTT for IoT over HTTP & UDP
- MQTT is very lightweight. The message size in MQTT is very small in comparison to HTTP. HTTP a lot of extra, unnecessary fields that is sent in the request & response messages that is not required for IoT.
- MQTT is reliable. UDP is a a connection less protocol as compared to TCP/IP which is connection oriented. UDP based protocols like MQTT-SN & CoAP are faster in sending messages but they are not reliable as the messages being sent to the broker are not acknowledged.
- MQTT is scalable. Although a bit tricky, it can be scaled to handle millions of messages.
- The big cloud providers use MQTT. AWS, Azure, OpenStack have IoT modules which support MQTT messaging.
This doesn’t mean that MQTT is the best in every scenario. If you have to compromise reliability for speed then UDP based protocols like CoAP, may be better.
5. Getting Started With MQTT
If you want to jump right into programming MQTT clients, here is a tutorial of using Paho MQTT in Python:
I have explained other topics in MQTT in this guide:
Guide to Paho Python MQTT Client