Publish-subscribe (pubsub) pattern

Publish-subscribe is a design pattern in which a centralized component acts as an intermediary between two distinct groups of software components: publishers and subscribers. Publishers send messages (typically in the form of events) to the central message broker, and the broker is responsible for delivering the messages to the subscribers who are interested in them.

Subscribers can subscribe to particular message channels or particular types of events or topics. Publishers have no knowledge of which components subscribe to their events, promoting [loose coupling].

A software component may be both a publisher and consumer, publishing some kinds of messages while consuming others.

The pubsub pattern is implemented in the brokers in event-driven systems. Event-driven architecture is a a style of application design in which components or services communicate with each other by producing and consuming events.

The pubsub pattern – and, by extension, event-driven architecture – is an implementation of the [dependency inversion principle]. The high-level components (publishers) are not dependent upon the low-level components (consumers), and vice versa. Instead, both types of components depend on an abstraction (the event messages themselves). This means that components (or services) can be more easily added, changed, replaced, without impacting the rest of the system.

See also the observer pattern, which is closely related to pubsub.