Publish-subscribe and broadcast¶
Category: Communication · Status: stub · Lessons: chapter 05, Message passing (planned)
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
- Is a kind of: Message passing
- See also: Reactive programming, Task queue
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¶
- In the books: Async JavaScript, Trevor Burnham — ch. 2, 'Distributing Events'
- In the books: Combine: Asynchronous Programming with Swift, Shai Mishali, Florent Pillet, Marin Todorov, Scott Gardner — ch. 2, 'Publishers & Subscribers'
- In the books: Kotlin Coroutines by Tutorials, Filip Babić, Nishant Srivastava — ch. 12, 'Broadcast Channels'
- In the books: Async Rust, Maxwell Flitton, Caroline Morton — ch. 6, 'Reactive Programming' → 'Enabling Broadcasting with an Event Bus'
- In the books: C++ Reactive Programming, Praseed Pai, Peter Abraham — ch. 11, 'Design Patterns and Idioms for C++ Rx Programming' → 'The Event bus pattern'
- In the books: Python Parallel Programming Cookbook, Giancarlo Zaccone — ch. 3, 'Process-based Parallelism' → 'Collective communication using broadcast'
- Notes: Broker - Asynchronous - in general ↗
- Reference: Wikipedia: Publish–subscribe pattern ↗