IoT MQTT Protocol

MQTT is a lightweight messaging protocol designed for devices with small memory and unreliable networks. It moves short messages quickly and uses little power, which explains why it became the favorite language of IoT. Smart home hubs, factory monitors, and fleet trackers rely on it daily.

The Publish and Subscribe Idea

MQTT does not connect devices directly. A central broker receives every message and forwards it to interested receivers.

Newspaper Comparison

A publisher prints a newspaper and never meets the readers. Readers subscribe to the titles they like, and the delivery service handles the rest. MQTT works the same way: senders publish messages to a topic, and receivers subscribe to topics.

[Temp sensor] ── publish "home/kitchen/temp" ──►┐
                                               ▼
                                          [ BROKER ]
                                               │
        subscribe "home/kitchen/temp"          │
   ┌───────────────────────────────────────────┤
   ▼                                           ▼
[Phone app]                              [Cooling fan controller]

Key Concepts

Broker

The broker is the central server that accepts messages and distributes them. Popular choices include Mosquitto and cloud-based brokers.

Topic

A topic is a text path that labels the message, such as farm/field1/moisture. Slashes create levels, like folders on a computer.

Publisher and Subscriber

The publisher sends data. The subscriber listens to chosen topics. One device can play both roles.

Payload

The payload is the message content, commonly plain text or a short JSON object.

Topic Wildcards

SymbolMeaningExample
+Matches one levelhome/+/temp reads every room
#Matches all remaining levelshome/# reads everything in the house

Quality of Service Levels

MQTT offers three delivery promises so developers match reliability to the job.

  • QoS 0: Deliver at most once. The message may vanish. Use it for frequent readings where one loss does not matter.
  • QoS 1: Deliver at least once. The receiver may see a duplicate. Use it for most sensor data.
  • QoS 2: Deliver exactly once. This level uses extra steps and suits billing or critical commands.

Special Features

Retained Messages

The broker stores the last message of a topic and gives it to every new subscriber. A new dashboard instantly shows the latest temperature.

Last Will and Testament

A device registers a farewell message when it connects. If the device disappears without warning, the broker publishes the message, such as "sensor offline".

Keep Alive

Devices send tiny pings so the broker knows they remain connected.

Sample Message

Topic:   factory/line2/motor1/temperature
Payload: {"value": 71.5, "unit": "C"}
QoS:     1

Why Developers Choose MQTT

  • Small packets save bandwidth and battery.
  • One broker serves thousands of devices.
  • Many languages offer simple client libraries.
  • Devices reconnect easily after network drops.

Security Practices

  1. Use TLS encryption on port 8883 instead of plain port 1883.
  2. Require a username and password or a device certificate.
  3. Limit each device to its own topics with access rules.
  4. Avoid secrets inside topic names.

Designing Good Topic Names

Clear topic names keep large systems organized. Start with the broadest category and move toward the detail, such as building/floor2/room5/temperature. Keep names lowercase, avoid spaces, and never start a topic with a slash. A consistent pattern lets new engineers guess topic names and lets wildcards collect exactly the data they need.

Quick Hands-On Idea

Install the free Mosquitto broker on a laptop or Raspberry Pi. Subscribe to a topic in one terminal window and publish a message from another window. Seeing a message appear instantly makes the publish and subscribe model click.

Key Takeaway

MQTT keeps IoT messaging simple: publish to a topic, subscribe to a topic, and let the broker carry the load. Learn topics, wildcards, and quality levels, and you can design a clean messaging system for any project.