Pub Sub

Publisher Subscriber Pattern for decoupling

Pub-Sub data flow
Part of Series: Javascript Patterns
Table of Contents

The Publish-Subscribe (Pub/Sub) pattern is a software design pattern that facilitates communication between different parts of your application without tight coupling. It establishes a central communication hub, often called a “message broker” or “event bus,” that enables components to:

Implementation

type Task<T> = (data: T) => void

export class PubSub<T> {
  #subscriber = new Set<Task<T>>()
  constructor() {}

  subscribe(task: Task<T>) {
    this.#subscriber.add(task)
    return () => {
      this.#subscriber.delete(task)
    }
  }

  notify(data: T) {
    for (const subscriber of this.#subscriber) {
      subscriber(data)
    }
  }
}

Usages

const pubSub = new PubSub<string>()

// Can have one more more subscriber
const unsubscribe = pubSub.subscribe((data) => {
  console.log(data)
})

// Event Emitted by some other part of the code
pubSub.notify("message1")
pubSub.notify("message2")
pubSub.notify("message3")

// In future, on certain condition: all the subscribers will unsubscribe
unsubscribe()

Topics

Next In Series: Better Error Handling in JavaScript / Typescript