Publisher Subscriber vs Observer

ref –

  • https://blog.sessionstack.com/how-javascript-works-the-publisher-subscriber-pattern-9edc62ef1a68
  • https://refactoring.guru/design-patterns/observer/typescript/example
  • Broker Demo

The broker demo is to describe the pub/sub broker. To run it:

yarn install
yarn develop
open browser at “localhost:8080”

First, let’s take a look at the Observer Pattern.

Conceptual example on Observer Pattern

In the observer pattern, observers are attached to a subject. When the subjects notifies them all, it calls update on the subjects. That’s how the subjects are all notified.

In other words, with the observer pattern, each observer is required to be attached to the subject, so that it can get a notification. However, the pub/sub pattern does not require this.

Difference between pub/sub and Observer

The pub/sub pattern uses a middleware that sits between the objects firing the event. In this case, the publishers and the subscribed objects.

The pub/sub pattern involves a middleware that is also referred to as the broker.

The pub/sub broker handles interactions between the publishers and the subscribers via data structures and its implementations.
Publishers publish contents or publications to the pub/sub broker and it handles the delivery of these contents to the appropriate subscriber.

The pub/sub broker also enables loose decoupling of publishers and subscribers and it supports many to many relationships between the publishers and the subscribers.

So unlike the observer pattern, the pub/sub pattern allows multiple publishers and multiple subscribers.

Example – React app that subscribes to certain topics and receives the data in the UI component

Let’s see how it is used.
In our case subscribers are clients such as a React app. In its UI component, it needs to receive data about what to change and update on certain pages.
So the React code may be like so:

subscribing on the UI

Callback to receive published data

callback will start receiving data once an entity publishes to racePageTopic.

We can also use the broker in other components to subscribe to different topics to receive different data.
Again, the callback would receive the data and update the UI accordingly:

callback will start receiving data once an entity publishes to investmentViewTopic.

The Broker

We have a Map structure that keeps track of a string key and an array of functions.
The key is the topic that different clients will subscribe to, and the array of functions keep track of the subscribers.

We use subscription to return an object that has two functionalities: subscribe and unsubscribe. It uses closure to access the needed topic name and subscriber.

When subscribe, we simply keep track of the topic name as keys in our Map, and then append subscribers (from clients) onto the value array.

The publishers will want to publish data according to topic name. This means all subscribers to that topic name, will receive that data.

Subscribe

We first to check whether we have an entry for this topic. If we do, simply push. If not, create one, then push the subscriber onto it.

Unsubscribe

We unsubscribe a client subscriber by iterating our values array to find it. Then remove it.

Publish

Backend apps can act as publishers and push data like so. All it needs is to know which topic it should push to.
Once we give a topic name, it’ll search for that topic, seems which subscribers are stored in the value array, and then
execute each subscriber with data as args.

Publishers

A node server may act as a publisher. It will import PSBroker, then use it to publish data
to all the subscribers for that event topic.