What is MQTT? Introduction to the MQTT protocol
Imagine a factory with hundreds of sensors monitoring temperature, humidity, and machine conditions. Each sensor sends readings to a monitoring dashboard, while other applications use the same data to detect problems or generate reports.
How can all these devices exchange information without connecting directly to every application? One answer is MQTT (Message Queuing Telemetry Transport), a lightweight messaging protocol designed for efficient communication between devices and applications.
What is MQTT?
MQTT is a lightweight messaging protocol based on the publish-subscribe model. Instead of devices communicating directly with each other, they exchange messages through a central message broker.
MQTT was developed in 1999 by Andy Stanford-Clark and Arlen Nipper to support communication over unreliable, low-bandwidth networks, particularly satellite connections used in the oil and gas industry.
Today, MQTT is widely used in Internet of Things (IoT) applications, including smart homes, industrial automation, connected vehicles, and remote monitoring.
What makes MQTT useful is its simplicity. It uses small message headers, with a minimum fixed header of just two bytes, reducing the amount of data transmitted over the network. This makes it suitable for devices with limited processing power, memory, or network bandwidth.
How does MQTT work?
MQTT communication involves three main concepts: clients, brokers, and topics.
- Clients: Devices or applications that publish or subscribe to messages. A temperature sensor can publish readings, while a monitoring dashboard subscribes to receive them. A client can do both.
- Broker: The server responsible for receiving messages and routing them to interested subscribers.
- Topics: Names used to organize messages and determine which subscribers should receive them.
The publish-subscribe model
Back to the factory example. A temperature sensor on Machine 1 publishes its readings to the topic factory/machine1/temperature.
Here’s how the communication works:
- The monitoring dashboard subscribes to
factory/machine1/temperature. - The sensor publishes a temperature reading, such as
28.5, to the broker. - The broker checks which clients have subscribed to that topic.
- The dashboard receives the reading and displays the temperature.

The sensor doesn’t need to know where the dashboard is running or how it processes the data. If another application needs the same readings, it can subscribe to the topic without requiring changes to the sensor. This separation between publishers and subscribers makes it easier to add devices and applications as a system grows.
Understanding MQTT topics
MQTT topics organize messages using a hierarchy separated by forward slashes (/).
For example, a factory might use:
factory/machine1/temperature
factory/machine1/humidity
factory/machine2/temperature
A subscriber can listen to an individual topic or use wildcards to receive messages from several topics. MQTT supports two wildcards:
| Wildcard | Meaning | Example |
|---|---|---|
+ |
Matches one topic level | factory/+/temperature |
# |
Matches zero or more levels | factory/# |
Subscribing to factory/+/temperature receives temperature readings from both machines. Subscribing to factory/# receives messages across the entire factory topic hierarchy.
Wildcards are used in subscriptions, not when publishing messages. Topics are also case-sensitive, meaning factory/temperature and Factory/temperature are different topics.

Key features of MQTT
MQTT was designed for environments where devices may have limited resources or unreliable network connections. Several features help applications handle these conditions.
1. Quality of Service: How reliably should messages be delivered?
Not every message needs the same level of reliability. A temperature sensor publishing a reading every second might tolerate an occasional lost message. An important machine status update may require stronger delivery guarantees.
MQTT provides three Quality of Service (QoS) levels:
| QoS | Delivery guarantee | How it works |
|---|---|---|
| QoS 0 | At most once | Sends the message without acknowledgement |
| QoS 1 | At least once | Uses acknowledgements and may deliver duplicates |
| QoS 2 | Exactly once | Uses a multi-step handshake to prevent duplicate delivery at the protocol level |
QoS 0 has the lowest overhead, making it useful for frequent updates where occasional message loss is acceptable. QoS 1 provides stronger delivery assurance but requires applications to handle possible duplicates. QoS 2 adds more protocol overhead to provide exactly-once delivery at the MQTT protocol level.
These guarantees apply to message transfer, not the successful completion of application processing. Receiving a message doesn’t guarantee that an application has successfully saved it to a database. A broker’s support for particular QoS levels also depends on its implementation.
2. Retained messages: Getting the latest value
Imagine a monitoring dashboard that opens just after a sensor has published its temperature reading, and the sensor only publishes every five minutes. Without a retained message, the dashboard might have to wait for the next reading.
MQTT allows a publisher to mark a message as retained. The broker stores the latest retained message for that topic and sends it when a client creates a matching subscription. This allows the dashboard to display the most recently reported temperature immediately.
A retained message is not a message history. The broker keeps only the latest retained message per topic.
3. Last Will and Testament: Detecting unexpected disconnections
When connecting, a client can register a message that the broker should publish if the connection ends unexpectedly. For example:
Topic: factory/machine1/status
Message: offline
If the sensor unexpectedly disconnects and the broker detects the connection loss, the broker publishes the message to the configured topic. Monitoring applications subscribed to that topic can then react accordingly.
The will is not published when the client disconnects normally using MQTT’s DISCONNECT packet. Last Will messages are useful for monitoring device connectivity, though a disconnected sensor doesn’t necessarily mean the machine itself has failed.
4. Persistent sessions: Handling temporary disconnections
Some devices disconnect temporarily because of unstable networks or power-saving behavior. MQTT supports persistent sessions, allowing a client to reconnect and continue using its previous subscriptions.
In MQTT 3.1.1, a client requests a persistent session by setting cleanSession to false. Depending on the QoS level and broker implementation, eligible messages can be queued while the client is offline and delivered when it reconnects.
Persistent sessions differ from retained messages:
- Retained messages provide the latest stored value for a topic to any new subscriber.
- Persistent sessions preserve subscription state and can retain messages intended for a temporarily disconnected client.
Both are useful, but they solve different problems.
When to use MQTT
MQTT is a good choice when devices need to exchange small messages efficiently, especially over networks with limited bandwidth or unreliable connectivity.
Common use cases include:
- Industrial IoT: Machines publishing temperature, vibration, and operational data.
- Smart homes: Sensors, lights, and controllers exchanging updates and commands.
- Remote monitoring: Devices sending measurements over cellular or other constrained networks.
- Connected applications: Dashboards receiving real-time updates from distributed devices.
However, MQTT isn’t the best fit for every messaging requirement. For applications that need work queues, advanced routing, or backend task processing, AMQP 0-9-1 may offer a better messaging model. In some systems, using both protocols makes sense.
Using MQTT with LavinMQ
LavinMQ is a message broker that supports MQTT alongside AMQP 0-9-1. It implements MQTT versions 3.1.0 and 3.1.1 natively, allowing MQTT clients to connect directly without an additional plugin or external MQTT broker.
LavinMQ supports retained messages, persistent sessions, Last Will messages, and QoS 0 and 1.
One limitation: although MQTT defines an exactly-once delivery level, LavinMQ does not support QoS 2. MQTT 5 is also not currently supported.
Try MQTT with LavinMQ
The following example uses the Mosquitto command-line tools to publish a temperature reading to a LavinMQ broker. These tools work with any compatible MQTT broker.
For this example, LavinMQ is running locally with its MQTT listener enabled on port 1883.
First, open a terminal and subscribe to temperature readings:
mosquitto_sub -h localhost -p 1883 \
-u guest -P guest \
-t "factory/machine1/temperature" \
-q 1
In another terminal, publish a reading:
mosquitto_pub -h localhost -p 1883 \
-u guest -P guest \
-t "factory/machine1/temperature" \
-m "28.5" -q 1
The subscriber displays:
28.5
The publisher sends a reading to LavinMQ, and the broker forwards it to the subscribed client. The example uses local development credentials. For production, use dedicated credentials, appropriate topic permissions, and TLS encryption.
Combining MQTT and AMQP in one system
Alongside displaying temperature readings, a backend application may need to store measurements and trigger alerts when a machine overheats.
Sensors can continue publishing using MQTT, while backend services consume the readings using AMQP. LavinMQ supports this through its x-mqtt-topic exchange type, which allows MQTT publications to be routed into AMQP queues.
For example, an AMQP queue bound with the MQTT topic filter factory/+/temperature can receive temperature readings published by both machines. The backend application then consumes messages from that queue using AMQP.
This gives IoT devices a lightweight communication protocol while allowing backend services to use AMQP’s queue-based processing model. The integration is directional: MQTT publications can reach AMQP consumers through this exchange type, but AMQP publications are not automatically forwarded to MQTT subscribers.
For configuration details and supported features, see the LavinMQ MQTT documentation.
Frequently asked questions
Does MQTT require a broker?
Yes. Standard MQTT publish-subscribe communication uses a broker to route messages between publishers and subscribers. Clients don’t need direct connections to one another.
What is the difference between MQTT and AMQP?
MQTT focuses on lightweight publish-subscribe messaging, making it useful for connected devices and constrained networks. AMQP 0-9-1 provides exchanges, queues, bindings, and consumer acknowledgements, which are useful for backend messaging and task processing.
Can LavinMQ be used as an MQTT broker?
Yes. LavinMQ supports MQTT 3.1 and 3.1.1 natively. Clients can connect using the MQTT listener, typically on port 1883, or over TLS on port 8883 when configured.
Does MQTT guarantee message delivery?
MQTT provides different delivery guarantees through QoS levels, but no QoS setting guarantees successful application processing. Reliability also depends on broker behavior, connection state, persistence, and application design.
Conclusion
MQTT provides a simple way for devices and applications to communicate without connecting directly to each other. Its lightweight design and features such as QoS, retained messages, Last Will messages, and persistent sessions make it useful for IoT applications operating under different network conditions.
For systems that also need backend message processing, LavinMQ allows MQTT devices and AMQP consumers to work with the same broker.
Explore MQTT with LavinMQ or get started with a managed LavinMQ instance on CloudAMQP.
Nyior Clement