MQTT is the unsung hero of IoT fleet management because it allows devices to talk to each other without a central orchestrator, making your fleet incredibly resilient.

Imagine you have a dozen temperature sensors scattered across a warehouse, each sending data every minute. You also have a few actuators, like fans, that you need to turn on if a temperature threshold is breached.

Here’s how that might look in action using MQTT:

Sensor Publishing Data: A temperature sensor, let’s call it warehouse/zone1/temp, publishes a message with the payload 25.5 to the topic warehouse/zone1/temp.

Broker: An MQTT broker (like Mosquitto, EMQ X, or HiveMQ) is listening.

Commanding an Actuator: A separate application, perhaps a dashboard, wants to turn on a fan in warehouse/zone1. It publishes a message ON to the topic warehouse/zone1/fan/command.

Actuator Subscribing: The fan actuator, subscribed to warehouse/zone1/fan/command, receives the ON message and activates the fan.

Data Aggregation: Another application, designed to monitor overall warehouse temperature, subscribes to warehouse/#. This wildcard subscription means it receives messages from any topic starting with warehouse/. It gets the 25.5 from warehouse/zone1/temp and potentially other sensor readings from warehouse/zone2/temp, warehouse/zone3/temp, etc.

This pub/sub model means sensors don’t need to know about the dashboard, and the dashboard doesn’t need to know the individual IP addresses of the sensors or actuators. They only need to know the broker’s address and the topics they’re interested in.

The fundamental problem MQTT solves is decoupling. In traditional client-server models, a client needs to know the server’s address, and the server needs to know about all its clients. If a client goes offline, other clients can’t communicate with it. If the server goes offline, nothing works. MQTT, with its broker acting as an intermediary, breaks these direct dependencies. Devices (clients) only need to know the broker. They publish messages to topics, and other devices subscribe to those topics. The broker routes messages from publishers to subscribers. This makes the system incredibly scalable and resilient. If a sensor goes offline, other sensors and the broker continue to function. If the broker goes offline, nothing works, but this is a single point of failure that can be mitigated with clustering.

You control the system primarily through topic design and Quality of Service (QoS) levels. Topic names are hierarchical, much like file paths (e.g., company/location/device_type/device_id/measurement). A well-designed topic structure is crucial for efficient routing and filtering. QoS levels determine the guarantee of message delivery:

  • QoS 0 (At most once): Fire and forget. Fastest, but messages can be lost.
  • QoS 1 (At least once): Guarantees delivery, but messages might be duplicated. The sender waits for an ACK.
  • QoS 2 (Exactly once): Guarantees delivery exactly once. Slowest, involves a handshake.

For fleet management, you’ll often use QoS 1 for critical commands (like turning a device off) to ensure it happens, and QoS 0 for frequent telemetry data where occasional loss is acceptable.

The most surprising mechanism for many is how MQTT handles disconnected clients and message persistence. If a client disconnects, the broker can be configured to store messages published to topics that the client was subscribed to. When the client reconnects, the broker can then deliver these stored messages. This is achieved through a combination of the clean session flag during client connection and the broker’s configuration for message retention. If a client connects with clean session = false, the broker will maintain its subscription state and deliver retained messages upon reconnection. This is vital for unreliable networks common in IoT, ensuring commands aren’t missed even if a device has intermittent connectivity.

The next concept you’ll grapple with is securing these MQTT communications, especially when dealing with a distributed fleet over public networks.

Want structured learning?

Take the full Mqtt course →