Early previewThis guide is in development. Items tagged verify are still being checked against official docs.
nebx Learn
GitHub
Menu
JS

JavaScript

The language of the web, on the client, the server and the edge.

Paradigm
Multi-paradigm, prototype-based, functional
Typing
Dynamic, weak (TypeScript adds static types)
Memory
Garbage collected
Runs
V8 / SpiderMonkey / JSC engines (JIT)
Concurrency
Single-threaded event loop, workers
Behavioral pattern

Observer

Intent

Defines a subscription mechanism that notifies multiple objects about events in the object they observe.

Problem

Customers keep polling the store for a new product, or the store spams everyone.

Solution

The publisher keeps a list of subscribers and notifies them when something happens. Events, signals and pub/sub are all Observer.

Real-world analogy

Think of it like this

A magazine subscription: new issues arrive when they're published, so you don't keep checking the shop.

Structure

association / extends implements / depends on
Publisher - subscribers: Subscriber[]- mainState+ subscribe(s)+ unsubscribe(s)+ notifySubscribers() 12 «interface» Subscriber + update(context) 3 ConcreteSubscribers + update(context) 4 Client 5

Hover or tap a numbered role to highlight it in the diagram.

  1. 1Publisher — Publisher issues events and keeps the subscription list.
  2. 2Publisher — When an event happens, it loops over subscribers and calls update().
  3. 3Subscriber — Subscriber declares the notification interface, usually a single update method.
  4. 4ConcreteSubscribers — Concrete Subscribers react to notifications.
  5. 5Client — Client creates publishers and subscribers and registers subscriptions.

Try it

InteractiveToggle subscribers, then publish an event. Only subscribed objects are notified.
OrderService EmailServiceInventoryAnalytics
Subscribers
// notifications appear here

Code example

observer.js
import { EventEmitter } from "node:events";

class Document extends EventEmitter {
  save(name) {
    // ... persist
    this.emit("saved", name);
  }
}

const doc = new Document();
doc.on("saved", (name) => console.log(`Saved ${name}`));
doc.save("report.txt");

Applicability

Use Observer when changes in one object require changes in others, unknown in advance.

Use Observer when some objects should observe others only for a time.

How to implement

  1. Split business logic into a publisher and subscribers
  2. Declare the subscriber interface (often a callback type)
  3. Add subscribe / unsubscribe to the publisher
  4. Notify subscribers when the event happens
  5. Subscribers register with publishers

Pros and cons

Pros

  • Open–Closed: new subscribers without changing the publisher
  • Relationships set up at runtime

Cons

  • Notification order is not guaranteed
  • Forgotten subscriptions can leak memory

Relations with other patterns

Check yourself

Check yourself

When an order is paid, the email, inventory and analytics modules must react, and more may be added later.

Saved in this browser only. Part of Behavioral patterns (0/11), Hands-on: patterns with live demos (0/5).