MQTT

Comprehensive study notes, diagrams, and exam preparation for MQTT.

MQTT

Definition

MQTT is a lightweight, open, publish/subscribe messaging protocol that enables devices and applications to exchange messages through a broker using topics, with minimal bandwidth usage and low overhead.

It was originally developed by IBM in the late 1990s for connections with low bandwidth and unreliable networks. Today, it is maintained as an open standard by OASIS and is one of the most important communication protocols in modern IoT systems.

Key characteristics of MQTT include:

Lightweight message structure

  • , which reduces network traffic

Broker-based communication

  • , which decouples senders and receivers

Topic-based routing

  • , where messages are delivered based on topic names

Quality of Service (QoS)

  • levels, which control message delivery reliability

Persistent sessions and retained messages

  • , which help handle intermittent connectivity

Main Content

1. Publish/Subscribe Architecture

  • In MQTT, devices are not connected directly to each other. Instead, they interact through a broker, which acts like a central message hub.
  • A device that sends data is called a publisher, and a device that receives data is called a subscriber. Both communicate using topics, which are hierarchical labels that organize messages.

This architecture is highly efficient because publishers only send one message to the broker, and the broker forwards it to all relevant subscribers. For example, a humidity sensor can publish data to home/livingroom/humidity, and any application subscribed to that topic will receive it immediately.

Benefits of this model include:

  • Loose coupling between devices and applications
  • Easy scalability as the number of devices grows
  • Simpler integration of new services without changing device logic

This is one of the main reasons MQTT is preferred in IoT systems where many devices must communicate in real time.

2. Topics, QoS, and Message Delivery

  • MQTT uses topics to organize communication. Topics are string-based paths such as city/traffic/intersection1 or vehicle/car42/speed, and subscribers can listen to exact topics or wildcard patterns.
  • MQTT supports three QoS levels to control reliability:
  • QoS 0: At most once delivery
  • QoS 1: At least once delivery
  • QoS 2: Exactly once delivery

Topics make MQTT flexible and easy to structure. For example, a smart home may use:

  • home/kitchen/light
  • home/bedroom/temperature
  • home/garage/door

QoS is important because different applications need different reliability:

QoS 0

  • is fastest and uses the least overhead, suitable for sensor updates where occasional loss is acceptable.

QoS 1

  • ensures the message arrives, but duplicates may occur.

QoS 2

  • provides the highest reliability by preventing duplicates, but it has more overhead and is slower.

MQTT also supports retained messages, where the broker stores the most recent message on a topic and sends it to new subscribers immediately. For example, if a thermostat publishes 22°C as a retained message, any new dashboard subscribing later will instantly get the latest temperature without waiting for the next update.

3. Connection Management and Session Features

  • MQTT is designed for unstable or constrained networks, so it includes features such as persistent sessions, keep-alive signals, and Last Will and Testament (LWT) messages.
  • These features help detect connection loss, preserve subscriptions, and notify others when a device disconnects unexpectedly.

A keep-alive interval tells the broker how often a client will communicate. If no packets are exchanged within that time, the broker can check whether the client is still alive. This is especially useful for battery-powered or remote devices.

A persistent session allows a client to reconnect and continue with previous subscriptions or queued messages, which is helpful in situations where connectivity is intermittent, such as mobile devices or remote sensors.

The Last Will and Testament feature is very useful in monitoring systems. If a device disconnects unexpectedly, the broker can automatically publish a predefined message. For example, a smart machine can publish machine/status/offline so that operators know it failed or lost connectivity.

Together, these features make MQTT reliable for real-world applications where devices may frequently go offline and reconnect.


Working / Process

1. A client connects to the broker

  • An MQTT client, such as a sensor, smartphone app, or backend service, opens a connection to the broker.
  • During connection, it may provide authentication credentials, client ID, keep-alive settings, and session options.

2. Clients publish and subscribe using topics

  • The publisher sends messages to a specific topic.
  • Subscribers register interest in one or more topics, possibly using wildcards such as + or # to match multiple topics.
  • The broker receives the published message and forwards it to all matching subscribers.

3. The broker manages delivery and session behavior

  • The broker applies QoS rules, stores retained messages, handles offline queues if enabled, and maintains session state.
  • It also supports disconnect handling, LWT publication, and message acknowledgments depending on the QoS level used.

Example workflow:

  • A weather station publishes temperature data to weather/station1/temp
  • A mobile app subscribes to weather/station1/temp
  • The broker delivers each new reading to the app
  • If the app reconnects later, it may receive the retained latest temperature immediately

This process is efficient because the publisher does not need to know who is listening, and subscribers do not need to know who sent the data.


Advantages / Applications

Very low bandwidth usage and minimal overhead

  • MQTT packets are small, making the protocol ideal for constrained devices and networks.
  • This efficiency helps reduce power consumption and communication costs.

Highly scalable and flexible communication model

  • The broker can support many publishers and subscribers simultaneously.
  • New applications can subscribe to existing topics without changing device code, which makes large systems easier to expand.

Extensive real-world use in IoT and monitoring systems

  • MQTT is used in smart homes, industrial automation, agriculture, fleet tracking, energy systems, healthcare monitoring, and remote diagnostics.
  • Example: A smart irrigation system can publish soil moisture readings, while an automation service subscribes to trigger watering only when necessary.

Additional important uses include:

  • Real-time chat and notification systems
  • Telemetry collection from vehicles and machines
  • Environmental monitoring with distributed sensors
  • Home automation platforms such as lighting, locks, and thermostats

Because of its low latency, reliability options, and broker-based design, MQTT is one of the most practical protocols for distributed communication in modern connected systems.


Summary

  • MQTT is a lightweight publish/subscribe messaging protocol mainly used for IoT and real-time communication.
  • It works through a broker that routes messages between publishers and subscribers using topics.
  • Its efficiency, QoS options, and connection features make it suitable for unreliable networks and constrained devices.
  • Important terms to remember: broker, publisher, subscriber, topic, QoS, retained message, keep-alive, last will