Master MQTT: From Protocol Basics to Powerful Client Tools
This article explains the lightweight publish/subscribe MQTT protocol, its origin, key features, usage scenarios, differences from traditional message queues, topic structures and wildcards, as well as practical client tools like Mosquitto and MQTTX with configuration examples.
MQTT
1. MQTT protocol description
Message Queuing Telemetry Transport (MQTT) is a lightweight message transport protocol based on the publish/subscribe model.
2. MQTT protocol origin
In 1999, Andy Stanford‑Clark and Arlen Nipper needed a protocol for satellite‑connected oil‑pipeline monitoring that minimized battery consumption and bandwidth, leading to the invention of MQTT.
3. MQTT protocol usage scenarios
It is suitable for IoT applications in low‑bandwidth and unstable network environments, providing real‑time reliable messaging with minimal code.
MQTT is widely used in IoT, mobile internet, smart hardware, vehicular networks, smart cities, telemedicine, power, oil and energy, and other fields.
4. MQTT protocol characteristics
Easy to implement
Lightweight and efficient; the smallest packet is only 2 bytes
Uses TCP for stable, reliable connections
Based on a publish‑subscribe model supporting duplex communication
Highly flexible and extensible
5. C/S and pub/sub difference
C/S model: client communicates directly with the server.
pub/sub: the publisher and subscriber are decoupled; communication is mediated by a broker.
6. Message filtering
The broker can filter messages so each subscriber receives only the messages it is interested in.
Filtering options:
1) Topic‑based filtering: each message has a topic; clients subscribe to topics of interest.
2) Content‑based filtering: the broker filters messages based on their payload content.
3) Type‑based filtering: common in object‑oriented languages, filtering by message (event) type.
7. MQTT vs. message queue
Message queue: a message can be consumed by only one client.
MQTT: every subscriber receives the message; all subscribers share the same payload.
8. MQTT client
Both publisher and subscriber are MQTT clients. The concept of publisher/subscriber simply indicates whether the client is sending or receiving messages.
MQTT clients are any devices running an MQTT library and connecting to an MQTT broker, ranging from micro‑controllers to full‑featured servers.
9. MQTT broker
The broker receives all messages, filters them, determines which clients subscribed to each message, and forwards them accordingly.
The broker also stores session data, handles authentication and authorization.
10. MQTT connection
The client initiates a connection by sending a CONNECT message; the broker responds with CONNACK and status codes. Once established, the connection remains open until the client disconnects or the link breaks.
11. Message type description
Publish message
After connecting, a client can publish messages. Each message includes a topic and a payload (data format defined by the publisher).
Subscribe message
The client sends a SUBSCRIBE message to the broker with the desired topics.
ACK messages
After subscribing, the broker replies with SUBACK. Unsubscribing is confirmed with UNSUBACK.
12. Topic related concepts
In MQTT, a topic is a UTF‑8 string used by the broker to filter messages for each client. Topics have a hierarchical structure separated by /.
Compared with traditional message queues, MQTT topics are lightweight; clients do not need to pre‑create topics before publishing or subscribing.
13. Topic wildcards
Clients can subscribe using exact topics or wildcards. Two types of wildcards exist:
Single‑level wildcard ( +) replaces one level of the topic hierarchy.
Multi‑level wildcard ( #) matches multiple levels and must be the last character in a topic, preceded by a /.
When a client subscribes with a multi‑level wildcard, it receives all messages that match the preceding part of the topic; using # alone receives every message.
MQTT client tools
1. Mosquitto
Mosquitto is a popular open‑source MQTT broker and client library that implements MQTT v5.0, v3.1.1, and v3.1.
After installation, Mosquitto provides two command‑line tools: mosquitto_pub and mosquitto_sub.
# Download
https://github.com/eclipse/mosquitto
# Official site
https://mosquitto.org/mosquitto_pub
Publishes a message to an MQTT broker.
Parameters:
-h # broker address (default localhost)
-p # broker port
-u # username for authentication
-P # password for authentication
-m # message payload
-t # topic name
-i # client identifierExample:
# Publish "Hello, MQTT" to topic "topic/test" on localhost
mosquitto_pub -h localhost -t "topic/test" -m "Hello, MQTT"
# Publish to a remote broker
mosquitto_pub -h 10.0.0.33 -t "/xyz" -i "client3" -m "How are you?" -u bruce -P lan1532mosquitto_sub
Subscribes to a topic on an MQTT broker.
# Subscribe to all messages on "topic/test" on localhost
mosquitto_sub -h localhost -t "topic/test"
# Subscribe with client ID and authentication
mosquitto_sub -h localhost -t "/xyz" -i "client1" -u bruce -P lan1532Configuration file example (excerpt):
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
allow_anonymous false
password_file /mosquitto/pwdfile
acl_file /mosquitto/aclfile
port 1883
protocol mqtt
max_connections -1To create user credentials:
# Create a new password file with user "tom"
mosquitto_passwd -c /mosquitto/pwdfile tom
# Edit ACL file to grant permissions
user tom
topic write /#
topic read /#2. MQTTX
EMQX is a widely used, large‑scale distributed IoT MQTT server.
MQTTX is an open‑source, cross‑platform desktop client that supports full MQTT 5.0 features and runs on macOS, Linux, and Windows.
# EMQX download page: https://www.emqx.io/zh/downloads?os=Windows
# Project repository: https://github.com/emqx/MQTTX
# Release page: https://github.com/emqx/MQTTX/releases
# Official site: https://mqttx.app/zhSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Community
A leading IT operations community where professionals share and grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
