Skip to content

Publish-subscribe and broadcast

Category: Communication · Status: stub · Lessons: chapter 05, Message passing

One line: A sender publishes to a topic and every subscriber gets its own copy, without the sender knowing who the subscribers are.

Also called: pub/sub, broadcast channel, message broker.

How it connects

flowchart LR
  n_message_passing["Message passing"]
  n_publish_subscribe["Publish-subscribe and broadcast"]
  n_publish_subscribe -->|is a| n_message_passing
  classDef center stroke-width:3px
  class n_publish_subscribe center
  classDef outside stroke-dasharray: 4 3
  class n_message_passing outside

In each language

Rust not in std; tokio's broadcast ↗ is a multi-producer, multi-consumer queue in which every receiver sees each value
Java SubmissionPublisher ↗, a Flow.Publisher that issues each item to all current subscribers, each with its own buffer
C# IObservable<T> ↗, a provider for push-based notification in the observer design pattern
JavaScript BroadcastChannel ↗ delivers to every listener of the same origin except the sender; Node's EventEmitter ↗ calls every listener attached to an event, synchronously
Kotlin SharedFlow ↗, a hot flow that shares emitted values among all its collectors
Swift Combine's publishers and subscribers ↗
Erlang and Elixir Erlang's pg ↗, distributed named process groups whose members a sender looks up with get_members; Elixir's Registry ↗ can serve as a local PubSub
Elsewhere Redis Pub/Sub ↗ has at-most-once delivery: a message is delivered once if at all

Where to read more