Publish-subscribe (pub/sub) is a messaging pattern where publishers push messages to subscribers. In software architecture, pub/sub messaging provides instant event notifications for distributed applications, especially those that are decoupled into smaller, independent building blocks. In laymen’s terms, pub/sub describes how two different parts of a messaging pattern connect and communicate with each other.

How Pub/Sub Works

Realtime API Publish-Subscribe Messaging Pattern
Figure 1.1 – An example of a publish/subscribe messaging pattern

These are three central components to understanding pub/sub messaging pattern:

  1. Publisher: Publishes messages to the communication infrastructure
  2. Subscriber: Subscribes to a category of messages
  3. Communication infrastructure (channel, classes): Receives messages from publishers and maintains subscribers’ subscriptions.

The publisher will categorize published messages into classes where subscribers will then receive the message. Figure 1.1 offers an illustration of this messaging pattern. Basically, a publisher has one input channel that splits into multiple output channels, one for each subscriber. Subscribers can express interest in one or more classes and only receive messages that are of interest.

The thing that makes pub/sub interesting is that the publisher and subscriber are unaware of each other. The publisher sends messages to subscribers, without knowing if there are any actually there. And the subscriber receives messages, without explicit knowledge of the publishers out there. If there are no subscribers around to receive the topic-based information, the message is dropped.

Topic and Content-Based Pub-Sub Models

In the publish–subscribe model, filtering is used to process the selection of messages for reception and processing, with the two most common being topic-based and content-based.

In a topic-based system, messages are published to named channels (topics). The publisher is the one who creates these channels. Subscribers subscribe to those topics and will receive messages from them whenever they appear.

In a content-based system, messages are only delivered if they match the constraints and criteria that are defined by the subscriber.

Example

Amazon simple notification service

You can check out Amazon’s SNS for a simple example of a pub-sub pattern. They provide a simple notification service (SNS) that is managed by pub/sub messaging and mobile notifications service for coordinating the delivery of messages to subscribers. In other words, SNS allows you to push messages to a large amount of subscribers, distributed systems, services, and mobile devices. This makes it easy to push notifications (updates, promos, news) to iOS, Android, Fire OS, Windows and Baidu-based devices.

What are the benefits of pub/sub?

  1. Loose coupling: Publishers do not know the identities of the subscribers or the message types.
  2. Improved security: The communication infrastructure only publishes messages to the subscribed topics.
  3. Improved testability: Topics reduce the number of messages required for testing.

Resources

Wikipedia Publish-Subscribe Pattern

Enterprise Integration Patterns: Publish-Subscribe Messaging

Amazon Pub/Sub Messaging

Fanout Pub/Sub Protocols

Components Responsibilities Collaborations
Publisher Publishes messages to the communication infrastructure Publisher publishes messages and subscriber receives messages from topic subscriptions.
Subscriber Subscribes to one or more topics and receives messages from them Receives messages from the communication infrastructure
Communication infrastructure Receives messages from publishers and maintain subscribers’ subscriptions Transports published messages from publishers to subscribers

Microsoft Dev Source