MQTT: Publish & Subscribe Pattern Explained | Part 2
Tags: iot, mqtt, beginners
Published: 2022-07-05 19:23:04

In part 1 of this series, we introduced the IoT protocol MQTT. In this post, you can learn **MQTT's publish subscribe pattern.**

## MQTT Publish/Subscribe (PubSub) Pattern

The publish/subscribe pattern (also known as pub/sub) provides an alternative to traditional client-server architecture. In the client-sever model, a client communicates directly with an endpoint.The pub/sub model decouples the client that sends a message (the publisher) from the client or clients that receive the messages (the subscribers). The publishers and subscribers never contact each other directly. In fact, they are not even aware that the other exists. The connection between them is handled by a third component (the broker). The job of the broker is to filter all incoming messages and distribute them correctly to subscribers. So, let’s dive a little deeper into some of the general aspects of pub/sub (we’ll talk about MQTT specifics in a minute).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5ne61h5gg4yv64yv7no.png)

The most important aspect of pub/sub is the decoupling of the publisher of the message from the recipient (subscriber). This decoupling has several dimensions:

- Space decoupling: Publisher and subscriber do not need to know each other (for example, no exchange of IP address and port).

- Time decoupling: Publisher and subscriber do not need to run at the same time.

- Synchronization decoupling: Operations on both components do not need to be interrupted during publishing or receiving.

In summary, the pub/sub model removes direct communication between the publisher of the message and the recipient/subscriber. The filtering activity of the broker makes it possible to control which client/subscriber receives which message. The decoupling has three dimensions: space, time, and synchronization.

Watch this video for a visual understanding of the protocol.

{% embed https://youtu.be/HCzQJMdHcy0 %}

## Why PubSub?

### Scalability
Pub/Sub scales better than the traditional client-server approach. This is because operations on the broker can be highly parallelized and messages can be processed in an event-driven way. Message caching and intelligent routing of messages are often a decisive factors for improving scalability. Nonetheless, scaling up to millions of connections is a challenge. Such a high level of connections can be achieved with clustered broker nodes to distribute the load over more individual servers using load balancers. (This topic is beyond the scope of the current article, we’ll cover it in a separate post).

### Message filtering
It’s clear that the broker plays a pivotal role in the pub/sub process. But how does the broker manage to filter all the messages so that each subscriber receives only messages of interest? As you’ll see, the broker has several filtering options:

#### OPTION 1: SUBJECT-BASED FILTERING
This filtering is based on the subject or topic that is part of each message. The receiving client subscribes to the broker for topics of interest. From that point on, the broker ensures that the receiving client gets all message published to the subscribed topics. In general, topics are strings with a hierarchical structure that allow filtering based on a limited number of expressions.

#### OPTION 2: CONTENT-BASED FILTERING
In content-based filtering, the broker filters the message based on a specific content filter-language. The receiving clients subscribe to filter queries of messages for which they are interested. A significant downside to this method is that the content of the message must be known beforehand and cannot be encrypted or easily changed.

#### OPTION 3: TYPE-BASED FILTERING
When object-oriented languages are used, filtering based on the type/class of a message (event) is a common practice. For example,, a subscriber can listen to all messages, which are of type Exception or any sub-type.

## Key Advantages of MQTT

- MQTT decouples the publisher and subscriber spatially. To publish or receive messages, publishers and subscribers only need to know the hostname/IP and port of the broker.

- MQTT decouples by time. Although most MQTT use cases deliver messages in near-real time, if desired, the broker can store messages for clients that are not online. (Two conditions must be met to store messages: the client had connected with a persistent session and subscribed to a topic with a Quality of Service greater than 0).

- MQTT works asynchronously. Because most client libraries work asynchronously and are based on callbacks or a similar model, tasks are not blocked while waiting for a message or publishing a message. In certain use cases, synchronization is desirable and possible. To wait for a certain message, some libraries have synchronous APIs. But the flow is usually asynchronous. 

If you want to understand how MQTT is different from message queues, [read this article](https://www.hivemq.com/blog/mqtt-essentials-part2-publish-subscribe/). 

> Read the [next part of this blog series](https://dev.to/hivemq/mqtt-client-and-broker-connection-explained-part-3-29k1) to look more closely into what makes an MQTT client and a broker and how the two connect.

[Click here to read the original post](https://www.hivemq.com/blog/mqtt-essentials-part2-publish-subscribe/).

Get your copy of [MQTT Essentials eBook](https://www.hivemq.com/download-mqtt-ebook/?utm_source=content+syndication&utm_medium=devto&utm_campaign=MQTT+Essentials) to understand the protocol in detail without having to read the entire specification.