How Publisher-Subscriber Pattern works?
In Publisher-Subscriber pattern, the publisher sends the messages and subscriber receives the messages. But both publisher and subscriber do not know about each other. That means the publisher does not know about the subscribers and subscribers does not know about the publishers. Both of them communicated through a middle layer known as message broker (message broker will have the knowledge of both publisher and subscribers).
e.g:- Sending and Receiving messages using RabbitMQ message broker can be identified as a real world example of Publisher-Subscriber pattern.
The message broker will accept the messages from publisher and filter them based on some criteria and will do the required steps for delivering them to the right subscriber(s). The message will be filtered based on different criteria depending on the underlying messaging broker implementation. e.g:- In RabbitMQ, there will be different exchanges as Direct, Topic, FanOut and Header. Click here if you want to see my articles on RabbitMQ message broker.
Are Publisher-Subscriber pattern and Observer Pattern same?
First of all i want to emphasize that Publisher-Subscriber Pattern is NOT the Observer Pattern. They are two different design patterns with some similar behaviors.
The Subject in the Observer Pattern looks like the Publisher and the Observer can totally be related to a Subscriber of the Publisher-Subscriber Pattern. The Subject notify the Observers as how the Publisher generally notify its subscribers. That’s why most of the Design Pattern books or articles use ‘Publisher-Subscriber‘ notion to explain Observer Design Pattern. But there is another popular Pattern called ‘Publisher-Subscriber’ and it is conceptually very similar to the Observer pattern. In this article, we are going to learn about the Publisher-Subscriber Pattern.
The differences between Publisher-Subscriber and Observer pattern can be shown in the following diagram.
Lets summarize the differences between Publisher-Subscriber pattern and Observer pattern as follows.
- In the Observer pattern, Subject maintains a list of Observers and therefore Subjects aware of how to notify observers when there is a state change occurs. Whereas, in Publisher/Subscriber, publishers and subscribers don’t need to know each other. They simply communicate with the help of middle layer message broker (or message queue).
- In Publisher/Subscriber pattern, components are completely decoupled with compared to Observer pattern (In Observer pattern, Subject and Observers are loosely coupled).
- Observer pattern is mostly implemented in a synchronous way, i.e. the Subject calls the appropriate method of all its observers when some event occurs. The Publisher/Subscriber pattern is mostly implemented in an asynchronous way (using message queue).
- Observer pattern need to be implemented in a single application address space. It will be implemented within the single application. On the other hand, Publisher/Subscriber pattern is more of a cross application pattern. Publisher and Subscriber may resides in two different applications. each of them communicate over message broker or message queue.
Despite of the differences between these patterns, some might say that Publisher-Subscriber pattern is a variation of Observer pattern because of the conceptual similarity between them.