MQTT is the silent backbone of most smart home setups, and it’s surprisingly simple once you see it in action.

Imagine you have a smart light bulb and a smart switch. You want the switch to control the light. Instead of the switch directly talking to the light (which would require them to know each other’s IP addresses and be on the same network, a nightmare to manage), they both talk to a central server called an MQTT broker.

Here’s a simplified look at that communication:

1. The Broker: This is the central hub. Think of it like a post office. It doesn’t care who sends a message or who receives it, only that messages get to the right "mailboxes." We’ll run one locally using Mosquitto.

# Install Mosquitto on a Debian/Ubuntu system
sudo apt update && sudo apt install mosquitto -y

# Start and enable the service
sudo systemctl start mosquitto
sudo systemctl enable mosquitto

2. Topics: These are like the addresses for the mailboxes. They are human-readable strings that describe what the message is about. A common pattern is home/room/device/state.

  • A light might publish its status to home/livingroom/light1/state.
  • A switch might subscribe to home/livingroom/light1/state to know when to turn on/off.
  • The switch might publish a command to home/livingroom/light1/set to tell the light what to do.

3. Publishing and Subscribing:

  • Publishing: A device sends a message to the broker on a specific topic.

    • Your light bulb, when turned on via its app, publishes:
      # Using mosquitto_pub to simulate a light turning on
      mosquitto_pub -h localhost -t "home/livingroom/light1/state" -m "ON"
      
      Here, -h localhost means connect to the broker on the same machine, -t is the topic, and -m is the message payload.
  • Subscribing: A device tells the broker, "I want to receive all messages published to this topic."

    • Your smart switch, wanting to control light1, subscribes:
      # Using mosquitto_sub to listen for state changes
      mosquitto_sub -h localhost -t "home/livingroom/light1/state"
      
      Now, whenever mosquitto_pub sends a message to home/livingroom/light1/state, mosquitto_sub will receive and display it.

4. Controlling Devices: To turn the light on from the switch, the switch publishes a command to a different topic, often suffixed with /set or /command.

  • The switch publishes a command:
    # Using mosquitto_pub to simulate a switch sending a command
    mosquitto_pub -h localhost -t "home/livingroom/light1/set" -m "ON"
    
  • The light bulb, which is subscribed to home/livingroom/light1/set, receives this "ON" message and turns itself on.

This publish/subscribe model decouples devices entirely. The light doesn’t need to know the switch exists, and the switch doesn’t need to know the light’s IP address. They only need to agree on the broker’s address and the topics they’ll use. This makes adding new devices incredibly easy: just install the device, configure its broker address, and tell it which topics to listen to or publish on.

The broker also handles different levels of "quality of service" (QoS). QoS 0 means "send it and forget it" (fastest, but messages can be lost). QoS 1 means "at least once" (the sender gets an acknowledgment, so the message is guaranteed to arrive, but might arrive multiple times). QoS 2 means "exactly once" (the sender and receiver perform a handshake to ensure the message arrives precisely one time). For most home automation, QoS 1 is a good balance.

The real magic happens when you have multiple devices and complex automations. For example, you can set up a rule in Home Assistant (a popular home automation platform) that subscribes to your motion sensor’s topic (home/hallway/motionsensor/state) and, when it detects motion (MOTION), publishes an "ON" command to the hallway light’s topic (home/hallway/light1/set).

The most surprising thing is how resilient this system is to network changes. If your light bulb loses its Wi-Fi connection for a few minutes and then reconnects, it will automatically re-subscribe to its topics on the broker. Any commands it missed while offline can be resent by the broker if configured to do so (depending on the "retain" flag, which we won’t get into now).

Next, you’ll likely want to explore how to secure your MQTT broker with usernames, passwords, and TLS encryption.

Want structured learning?

Take the full Mqtt course →