PubSub module provides utilities for building publish-subscribe systems where publishers can send messages to many subscribers concurrently.
Overview
APubSub<A> is an asynchronous message hub where:
- Publishers can publish messages of type
A - Subscribers can subscribe to receive messages
- Multiple subscribers can receive the same messages
- Supports various backpressure strategies
- Handles concurrent access safely
Creating PubSub
Bounded PubSub
Creates a PubSub with backpressure - publishers wait when full:Unbounded PubSub
Creates a PubSub without capacity limits:Dropping PubSub
Drops new messages when full:Sliding PubSub
Drops oldest messages when full:Publishing Messages
Publish Single Message
Publish Multiple Messages
Subscribing to Messages
Basic Subscription
Multiple Subscribers
Taking Messages
Take Single Message
Take Multiple Messages
Take All Available
Integration with Streams
Broadcasting Domain Events
Shutdown and Cleanup
Capacity and Size
Best Practices
- Use scoped subscriptions: Always subscribe within Effect.scoped for automatic cleanup
- Choose appropriate capacity: Size PubSub based on producer/consumer rates
- Select the right strategy: Use bounded for backpressure, dropping/sliding for lossy scenarios
- Handle shutdown gracefully: Shutdown PubSub when no longer needed
- Use with Streams: Convert to Stream for powerful composition
- Avoid blocking subscribers: Process messages asynchronously to prevent blocking