Webinar
EMQX Cloud Message History: From MQTT Topics to SQL Queries →
Back

Introduction to the MQTT Publish-Subscribe Pattern

EMQX TeamEMQX Team
Dec 4, 2026MQTT
Introduction to the MQTT Publish-Subscribe Pattern

What Is the MQTT Publish-Subscribe Pattern?

The publish-subscribe (pub/sub) pattern is a messaging model that decouples message producers (publishers) from message consumers (subscribers). Instead of communicating directly, publishers and subscribers exchange messages through an intermediary, allowing them to operate independently without knowing each other's identities or network locations.

MQTT uses this pattern with a central component called the MQTT Broker. Publishers send messages to the broker under specific topics, while subscribers subscribe to topics to receive messages of interest. The broker handles message routing and delivery, eliminating the need for direct connections between publishers and subscribers.

Unlike systems that require topics or channels to be explicitly created in advance, MQTT allows clients to publish to and subscribe to topics dynamically. The broker matches incoming messages against existing subscriptions and forwards them to matching subscribers.

MQTT’s publish-subscribe model is event-driven, in which messages are routed asynchronously based on existing subscriptions. When a message is published, the broker checks it against the subscriptions currently in place and forwards it to matching subscribers. If no matching subscribers are available, the message is generally not delivered to any client.

MQTT does not automatically store messages for clients that have not subscribed yet. Features such as Retained Messages and Persistent Sessions provide separate mechanisms for storing messages or maintaining session state to support later delivery. Their behavior depends on the MQTT version, QoS level, session configuration, and broker implementation.

The MQTT publish-subscribe model consists of four main components: publishers, subscribers, brokers, and topics.

  • Publisher
    A publisher sends messages to the broker under a specified topic. Each MQTT PUBLISH packet is associated with one topic. Publishers do not need to know which clients are subscribed to that topic or whether those clients are currently online.
  • Subscriber
    A subscriber receives messages by subscribing to one or more topics. It can subscribe to multiple topics simultaneously and use wildcards to match a range of topics. MQTT also supports shared subscriptions, which allow multiple subscribers to share the workload of processing messages from the same subscription.
  • Broker
    The broker receives messages from publishers and routes them to matching subscribers. It also manages client connections and handles subscription and unsubscription requests, as well as message delivery according to MQTT's QoS and session mechanisms.
  • Topic
    A topic is a hierarchical string used by MQTT to classify messages and determine where they should be routed. Topic levels are separated by forward slashes (/), similar to URL paths. For example, sensor/1/temperature could represent temperature readings from a particular sensor.
    Multiple publishers can publish to the same topic, and multiple subscribers can subscribe to it. The broker uses topic names and subscription filters to determine which clients receive each message. Subscribers can also use topic wildcards to subscribe to multiple topics with a single subscription. Learn more in MQTT Topics and Wildcards: A Beginner's Guide.

MQTT Publish-Subscribe Architecture

Unlike some messaging systems, MQTT does not require Topics to be created or registered before messages can be published to them. A publisher can send a message to any valid Topic Name, and the broker routes it based on matching subscriptions. Subscribers, however, must establish their subscriptions before the relevant messages are published to receive them.

How Message Routing Works in MQTT

In MQTT, a client can act as a publisher, a subscriber, or both. When a publisher sends a message, the broker checks the message's topic against its active subscriptions and forwards the message to clients whose subscription filters match.

For example, a temperature sensor might publish readings to factory/line1/temperature. An application subscribed to factory/line1/temperature receives those readings, while another application subscribed to factory/line2/temperature does not.

Publish-subscribe systems generally support two approaches to message filtering and routing:

  • Topic-based routing
    Messages are routed according to their topics. Subscribers specify the topics they are interested in, and the broker forwards messages whose topics match their subscriptions. MQTT uses this approach as its primary message-routing mechanism.
  • Content-based filtering
    Messages are filtered according to their content or attributes rather than topic names alone. Subscribers or the messaging system define conditions that messages must satisfy before being delivered. This approach is useful when applications need to filter messages based on values within the payload or other message metadata.

Beyond standard topic-based routing, broker like EMQX provides content-based message routing through its SQL-based Rule Engine. Rules can inspect incoming MQTT messages, filter them based on conditions, transform their data, and route the results to other destinations through configured actions and integrations. This allows applications to implement more flexible data processing and routing without requiring all logic to reside in individual clients.

For details, see the EMQX Rule Engine documentation.

MQTT Publish-Subscribe vs. HTTP Request-Response

HTTP and MQTT are both widely used communication protocols, including in IoT applications, but they follow different communication models.

HTTP typically uses a request-response model: a client sends a request to a server, and the server returns a response. To receive continuously updated device data, an application using HTTP may need to poll the server periodically.

The two protocols also differ in their communication overhead and connection management:

  • Communication overhead: MQTT is designed for lightweight messaging, with a small fixed header and compact control packets. HTTP generally carries more protocol metadata, although actual overhead depends on the HTTP version, headers, and communication pattern.
  • Connection management: MQTT maintains persistent connections between clients and the broker, allowing messages to be delivered over an established connection. HTTP commonly follows a request-response interaction, although modern versions also support persistent connections and streaming.
  • Message delivery: MQTT provides QoS levels and session mechanisms to support different delivery requirements, including message delivery to temporarily disconnected clients under appropriate session and QoS configurations.

These differences make MQTT particularly suitable for scenarios involving large numbers of connected devices, constrained networks, and continuous data exchange. HTTP remains useful for request-driven interactions, web APIs, and application integrations. In practice, the two protocols are often used together.

MQTT's loosely coupled publish-subscribe model also means that publishers do not receive confirmation that subscribers have processed their messages. For applications that need a response from another client, MQTT 5.0 introduces a standardized Request-Response pattern using Response Topic and Correlation Data. This allows clients to build request-response interactions on top of MQTT's messaging infrastructure.

Learn more in MQTT 5.0 Request-Response Explained.

MQTT vs. Message Queues

MQTT and message queues share some concepts, including asynchronous messaging and the ability to decouple producers from consumers. However, they are designed for different communication needs.

MQTT is a lightweight messaging protocol designed primarily for communication between connected devices and applications.

Traditional message queue systems, on the other hand, are commonly used for asynchronous communication and task processing between backend services. They often emphasize message persistence, buffering, and reliable processing across distributed applications.

The two are not mutually exclusive. In many IoT architectures, MQTT brokers handle device connectivity and message distribution, while message queues connect the MQTT infrastructure to backend services and data-processing systems.

For example, an MQTT broker can receive telemetry from thousands of connected devices and route relevant data to a message queue. Backend applications can then consume and process these messages independently, helping separate device communication from business logic.

MQTT topics also differ from pre-configured queues in many traditional messaging systems. MQTT clients can publish to topics without creating them in advance. However, messages are not automatically stored simply because they have been published. Their delivery and persistence depend on subscriptions, QoS, retained messages, and session settings.

Conclusion

The publish-subscribe pattern is at the heart of MQTT's communication model. By decoupling publishers and subscribers through a broker, it enables devices and applications to exchange data without maintaining direct connections. This flexible architecture makes it possible to build scalable IoT systems with diverse communication needs.

With this foundation in place, you can explore how clients establish connections with an MQTT broker in our next article: https://www.emqx.com/en/blog/how-to-set-parameters-when-establishing-an-mqtt-connection

Talk to an Expert
Contact Us →