1. What is MQTT?
MQTT (Message Queuing Telemetry Transport) is a:
- Lightweight publish/subscribe messaging protocol
- Designed for low bandwidth and unstable network environments
- Particularly suitable for Internet of Things (IoT) device communication
Plain Language Explanation:
Imagine a delivery station (Broker), and you tell it: “I want to receive milk.” When the milk arrives, the delivery station will notify you, so you don’t have to keep checking. You won’t receive deliveries that others ordered.
This is the core of the publish/subscribe model — precise, efficient, and non-intrusive.
2. The Role of the MQTT Server (Broker)
What does the Broker do?
- Receive messages: Information from publishers
- Forward messages: To all clients subscribed to that topic
- Manage connections: Maintain the online status of clients
Common MQTT Brokers:
| Name | Features |
|---|---|
| Eclipse Mosquitto | Lightweight, open-source, easy to deploy |
| EMQX | Domestic open-source, powerful, supports clustering |
| HiveMQ | Commercial version, strong stability, commonly used in enterprises |
| VerneMQ | Supports distributed systems, good stability |
Plain Language:
The Broker is the “intercom”; who speaks and who listens all depends on it to relay.
3. How MQTT Works
3.1 Three Core Roles
- Publisher: The entity/device that publishes messages
- Subscriber: The entity/device that receives messages
- Broker: The relay station
3.2 Communication Process
- The client connects to the Broker via TCP/IP
- The subscriber subscribes to a topic, for example,
<span>/home/temperature</span> - The publisher sends a message to that topic
- The Broker forwards the message to all subscribers
4. Core Features of MQTT Servers
4.1 Topics
Similar to folder paths:
<span>/home/temperature</span><span>/office/light</span>
Wildcard Support:
<span>+</span>: Matches a single level path (e.g.,<span>/home/+</span>)<span>#</span>: Matches multiple levels (e.g.,<span>/home/#</span>)
4.2 Quality of Service Levels (QoS)
| Level | Meaning | Characteristics |
|---|---|---|
| 0 | At most once | May be lost, fastest |
| 1 | At least once | Not lost but may be duplicated |
| 2 | Exactly once | Not lost, not duplicated, safest and slowest |
Plain Language:
- QoS 0 = Regular delivery, no signature
- QoS 1 = Delivery must occur, may deliver more than once
- QoS 2 = Precise delivery, only delivered once
4.3 Retained Messages
- The Broker will save the last message
- New subscribers will immediately receive this message upon connecting
Example Scenario:
The temperature sensor
<span>/home/temperature</span>sends<span>25°C</span>, and a new device that comes online immediately knows the current temperature.
4.4 Last Will Messages
- When a client disconnects unexpectedly, the Broker automatically publishes a “last will message”
- Notifying other devices that the client has gone offline
5. Deploying an MQTT Server (Using Mosquitto as an Example)
5.1 Installation (Ubuntu)
sudo apt update
sudo apt install mosquitto mosquitto-clients
sudo systemctl enable mosquitto
5.2 Configuration File <span><span>/etc/mosquitto/mosquitto.conf</span></span>
listener 1883
allow_anonymous true
5.3 Testing
Start Subscriber:
mosquitto_sub -h localhost -t "test/topic"
Start Publisher:
mosquitto_pub -h localhost -t "test/topic" -m "Hello MQTT"
Result:
Subscriber terminal displays: <span>Hello MQTT</span>