Implementing the Observer pattern in JavaScript
Tags: programming, observer, observerpattern, designpatterns
Published: 2018-11-28 01:32:03

---
title: Implementing the Observer pattern in JavaScript
published: true
tags: programming,observer,observer-pattern,design-patterns
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/854/1*AfB9Ttrf_P0Y3ERyfJiNAg.png)<figcaption>Shamelessly borrowed from Wikipedia</figcaption>

The observer pattern is a design pattern that observes an entity and on change, notifies all subscribers of the change. It is very simple to understand and is very effective for when a change in an entity needs to trigger many events.

#### How does the observer pattern work?

The observer pattern is a combination of two elements, a **Subject,** and an **Observer**. The Subject tracks the value of an entity and the Observer listens for changes tracked by the Subject.

When an entity tracked by the Subject changes, the Subject notifies the Observer of the change. One Subject can have many Observers, in which case it notifies all the Observers of the change in the entity. The observers can be subscribed to trigger events when the Subject notifies them.

#### So how do you implement the observer pattern in JavaScript?

Let’s start with the Subject, which is alternatively called an **Observable**. The subject contains a **state** and a **collection of observers** that it notifies to when the state changes.

The subject provides methods to register and unregister observers and a method to notify all the observers.

{% gist https://gist.github.com/nishanbajracharya/d56878bf9d5bf63d111677cebb11cef9 %}

Each observer has a notify method which the subject calls when it needs to notify observers for change in state.

Next, let’s look at the Observer. It simply only contains a **notify** method but we add a **subscribe** method that is used to subscribe an event to the Observer. When the Subject notifies the Observer of change in state, the Observer triggers the subscribers.

{% gist https://gist.github.com/nishanbajracharya/991386ce21d2a762ca474602c6022736 %}

The subscriber is a function that gets called when the Observer’s notify function is called.

#### How do you use it?

We create a new subject that tracks the value of an entity and an observer for the subject an subscribe to the observer. Then when there is a change in the subject’s state, all the observers are notified and their subscribers are triggered.

{% gist https://gist.github.com/nishanbajracharya/b1ba737e81ea2d93cb0c8cd72179b3ce %}

The subject has two observers and each observer is subscribed by two functions. When the observable changes the state by calling the **setState** method, both the observers are notified and each of their subscribers are triggered.

And that is how the observer pattern works and how it is implemented in JavaScript.

#### An alternate and simpler approach

While the above approach to implementing the observer pattern is more than enough, it requires creating at least one instance of the subject and one instance of the observer to work. So we can simplify the pattern using a single Observer class that maintains the state and also triggers the subscribers of the observer.

{% gist https://gist.github.com/nishanbajracharya/7979d5c2d41eaa3c556d95ce762cda9d %}

#### What are some practical use cases of the Observer Pattern?

When done correctly, the observer pattern can be very powerful and extremely helpful for broadcasting information across an application. Some use cases of the observer pattern can be like so:

- **Group Messaging:** When a message is sent to a group chat, all the participants of the chat receive the notification of the new message.
- **Website Newsletters:** All subscribers of a website newsletter receive email notifications when the website publishes a new newsletter.
- **Redux:** When a redux action is dispatched, all the subscribers of the store are notified.

**References and Further Learning**

- [Observer pattern - Wikipedia](https://en.wikipedia.org/wiki/Observer_pattern)
- [Design Patterns Observer Pattern](https://www.tutorialspoint.com/design_pattern/observer_pattern.htm)
- [The Observer Pattern in JavaScript explained | pawelgrzybek.com](https://pawelgrzybek.com/the-observer-pattern-in-javascript-explained/)