Understanding the MQTT Publish-Subscribe Model

March 15, 2026

Understanding the MQTT Publish-Subscribe Model

Understanding the MQTT Publish-Subscribe Model

At the heart of the MQTT protocol is the publish-subscribe (pub-sub) communication pattern. Unlike the traditional client-server model where clients communicate directly with a server (like in HTTP), pub-sub decouples the message sender (the publisher) from the message receiver (the subscriber). This is achieved through a central component called a broker. This article focuses on the pub-sub model itself. For a complete guide that covers this and all other aspects of MQTT in great detail, see our new Definitive Guide to the MQTT Protocol.

This architectural choice is what makes MQTT so powerful and scalable for IoT applications, from a simple smart home to a complex Industrial IoT (IIoT) deployment. It is the engine that drives the real-time, responsive nature of platforms like the MQTTfy dashboard.

The Three Key Roles

There are three main roles in any MQTT system:

  1. Publisher: A client that sends (publishes) messages. A good example is an ESP32 sensor device reporting data.
  2. Subscriber: A client that receives (subscribes to) messages, such as a dashboard application or a database logging service.
  3. Broker: The central server that receives all messages from publishers and routes them to the appropriate subscribers. Many people start by setting up a broker on a Raspberry Pi for their local network.

Crucially, a single MQTT client can act as a publisher, a subscriber, or both simultaneously.

Topics: The Address of Your Data

Publishers don't send messages directly to subscribers. Instead, they publish messages to a topic. A topic is simply a UTF-8 string that acts as an address or a label for a message, structured hierarchically like a file path.

For example, this is a common topic structure in a smart home setup: home/livingroom/temperature

Designing a clear and consistent topic hierarchy is one of the most important aspects of building a scalable MQTT system. A well-designed topic structure is self-documenting and makes it easy to manage data flows and security.

The Publish-Subscribe Flow

The process is simple, elegant, and incredibly efficient:

sequenceDiagram participant P as Publisher (Sensor) participant B as Broker participant S1 as Subscriber 1 (Dashboard) participant S2 as Subscriber 2 (Database) S1->>B: SUBSCRIBE to "home/livingroom/temperature" S2->>B: SUBSCRIBE to "home/livingroom/temperature" P->>B: PUBLISH "22.5" to "home/livingroom/temperature" B->>S1: FORWARD "22.5" B->>S2: FORWARD "22.5"
  1. Subscription: Clients connect to the broker and subscribe to the specific topics they care about.
  2. Publication: A publisher connects and sends a message (the payload) to a single, specific topic.
  3. Distribution: The broker checks its list of subscriptions and forwards the message to every client that has subscribed to that exact topic.

Deep Dive: The Subscriber and the Power of Wildcards

A subscriber's power comes from its ability to subscribe not just to a single, specific topic, but to a range of topics using wildcards. This is a fundamental feature that enables much of the flexibility in the pub-sub model.

There are two types of wildcards in MQTT:

  • Single-Level Wildcard (+): The plus sign is a placeholder for a single level in the topic hierarchy. It allows you to subscribe to topics that match a specific pattern.

    • Example: Imagine you have temperature sensors in multiple rooms: home/livingroom/temperature, home/bedroom/temperature, and home/kitchen/temperature.
    • To get the temperature from all rooms, you could subscribe to: home/+/temperature.
    • This would match all three topics, but it would not match home/garage/lights/status or home/livingroom/humidity.
  • Multi-Level Wildcard (#): The hash sign is a placeholder for multiple levels in the topic hierarchy. It must be placed at the very end of the topic string, and it matches everything from that point downwards.

    • Example: To get all sensor readings (temperature, humidity, light level, etc.) from the living room, you could subscribe to: home/livingroom/#.
    • This would match home/livingroom/temperature, home/livingroom/humidity, and even home/livingroom/media_player/power/status.
    • To get every single message flowing through the broker (useful for debugging, but dangerous in production), you can subscribe to just #.

Platforms like the MQTTfy dashboard leverage wildcards extensively. When you configure a dashboard to show all data from a specific factory line (factory1/line3/#), you are creating a subscription that uses a multi-level wildcard to gather all the necessary data in real-time.

The Central Role of the MQTT Broker

The broker is the brain and nervous system of a publish-subscribe architecture. Its performance, reliability, and features directly determine the capabilities of your entire IoT system. While its primary job is routing messages, a professional-grade broker does much more:

  • Authentication: Verifying the identity of every connecting client.
  • Authorization: Enforcing rules (ACLs) about which clients can publish or subscribe to which topics.
  • Session Management: Managing client sessions, including storing subscription data for disconnected clients.
  • Message Buffering: Holding messages for clients that are temporarily offline (when using higher QoS levels).

Choosing Your Broker: From DIY to Enterprise Scale

The choice of broker is a critical architectural decision. For a hobbyist project, an open-source broker like Mosquitto running on a local server is a great start. However, for any commercial product or business-critical IIoT application, the limitations of a self-managed, single-instance broker quickly become apparent. This is where a managed, enterprise-grade broker becomes essential.

A professional solution like the Synapse MQTT broker is designed to solve the challenges of running MQTT at scale:

  • High Availability and Clustering: A single broker is a single point of failure. If the server crashes, your entire system goes down. An enterprise broker runs in a cluster of multiple nodes. If one node fails, the others take over seamlessly, providing fault tolerance and 100% uptime.
  • Massive Scalability: A self-hosted broker can handle hundreds or maybe thousands of connections. A clustered, professionally managed broker like Synapse is architected to handle millions of concurrent IoT device connections and a massive throughput of messages, ensuring your platform can grow without performance bottlenecks.
  • Managed Security: Securing IoT devices is complex. A managed platform handles the difficult parts for you, offering built-in TLS encryption, integration with enterprise identity systems, and a secure-by-default configuration.
  • Zero Operational Overhead: Managing, patching, scaling, and monitoring a broker cluster is a full-time job. A managed broker service like the one powering MQTTfy handles all of this for you, freeing up your development team to focus on building your application, not on managing infrastructure.

The Power of Decoupling

This model provides three critical forms of decoupling, which are the primary benefits of using a pub-sub architecture.

Type of DecouplingDescriptionBenefit
Space DecouplingThe publisher and subscriber do not need to know each other's IP address or location.You can add or remove any number of publishers or subscribers without affecting the others. This makes the system highly scalable and flexible. A new real-time dashboard can be brought online to monitor data without any changes to the publishing devices.
Time DecouplingThe publisher and subscriber do not need to be running at the same time.The broker can store messages for offline subscribers (using QoS and persistent sessions), ensuring no data is lost if a client temporarily disconnects.
Synchronization DecouplingOperations on both sides do not need to be halted during publishing or receiving.This asynchronous, non-blocking nature is highly efficient for resource-constrained devices, allowing them to publish a message and immediately go back to sleep to conserve power.

Advanced Pub-Sub Concepts for Robust Systems

Beyond the basics of topics and wildcards, several key MQTT features are essential for building reliable and professional applications.

Retained Messages: The Last Known Good Value

Normally, if you subscribe to a topic, you will only receive messages that are published after you subscribe. But what if you need to know the most recent value immediately?

This is solved by the retain flag. When a publisher sends a message with the retain flag set to true, the broker not only sends it to all current subscribers but also stores that message. The next time a new client subscribes to that topic, the broker will immediately send it this "retained" message.

Use Case: A smart thermostat publishes its current operating state (heating, cooling, fan, off) to the topic home/hvac/state with the retain flag. When you open your smart home dashboard, it subscribes to that topic and instantly receives the retained message, allowing it to display the current state correctly without having to wait for the thermostat to publish a new message.

Caution: Use the retain flag judiciously. If you publish a high-frequency sensor reading (like a temperature value every second) with the retain flag, you will be constantly overwriting the retained message on the broker, adding unnecessary load. It is best used for slowly changing state information.

Quality of Service (QoS): Guaranteeing Message Delivery

Quality of Service (QoS) is a setting on each message that defines the guarantee of its delivery. This is a critical feature for building reliable systems.

  • QoS 0 (At most once): The default level. It's a "fire and forget" approach. The message is sent once, with no confirmation of receipt. This is the fastest method but offers no guarantee. It's ideal for high-frequency, non-critical sensor data where losing a single message is acceptable.
  • QoS 1 (At least once): This level guarantees that the message will be delivered at least one time. The sender will keep re-sending the message until it receives a PUBACK (publish acknowledgment) from the receiver. This is the most common QoS level, perfect for commands or events that must be received, where the receiving application can handle potential duplicates.
  • QoS 2 (Exactly once): The most reliable, but also the slowest level. It uses a four-part handshake to ensure the message is delivered exactly one time, with no duplicates. This is essential for critical applications like billing systems or financial transactions where a duplicate message would be catastrophic.

The performance and reliability of QoS depend heavily on the broker. A robust broker like the Synapse MQTT broker is optimized to handle high volumes of QoS 1 and 2 messages without performance degradation.



Frequently Asked Questions (FAQ)