pub struct EventBus { /* private fields */ }Expand description
A pub/sub event bus for broadcasting events to multiple subscribers
The event bus uses Tokio’s broadcast channel internally. This means:
- Multiple subscribers can receive the same event
- Late subscribers only receive events published after they subscribed
- If a subscriber falls behind, older events are dropped (lagged)
§Thread Safety
The EventBus is Clone and can be shared across threads. All clones
share the same underlying channel, so events published on one clone
are received by subscribers on all clones.
Implementations§
Source§impl EventBus
impl EventBus
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a new event bus with custom capacity
The capacity determines how many events can be buffered before slow subscribers start missing events (lagging).
Sourcepub fn subscribe(&self) -> Receiver<Event>
pub fn subscribe(&self) -> Receiver<Event>
Subscribe to events
Returns a receiver that will receive all events published after this subscription was created. Events published before are not received (this is not a replay channel).
Sourcepub fn publish(&self, event: Event) -> usize
pub fn publish(&self, event: Event) -> usize
Publish an event to all subscribers
Returns the number of subscribers that received the event. Note: A return value of 0 means no one is listening.
Sourcepub fn subscriber_count(&self) -> usize
pub fn subscriber_count(&self) -> usize
Get the number of active subscribers