event_bus.rs
A runtime-agnostic, async, and thread-safe event bus for Rust. Designed to be efficient, simple, and easy to use, allowing you to publish and subscribe to messages across threads and async tasks.
Features
- Runtime-agnostic: works with any async runtime (Tokio, async-std, smol, etc.)
- Thread-safe: multiple publishers and subscribers can safely coexist
- Async & Stream-based: subscribers implement
futures::Stream - Automatic cleanup: topics are removed when the last subscriber drops
- Minimal & simple API: just
EventBus::subscribeandEventBus::publish
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
= "0.3"
Usage Example
use EventBus;
use StreamExt;
async
Notes:
- Messages are published as
&[u8]; encoding/decoding is the user's responsibility. - Multiple subscribers to the same topic each get a copy of every message.
- When all subscribers of a topic are dropped, the topic is automatically cleaned up.
API Overview
EventBus::new() -> EventBus– create a new busEventBus::builder() -> EventBusBuilder– create a new bus builderEventBus::subscribe(&self, topic: &str) -> Subscription– subscribe to a topicEventBus::publish(&self, topic: &str, data: &[u8]) -> Result<(), PublishError>– publish a messageSubscriptionimplementsfutures::Stream<Item = Arc<[u8]>>
License
MIT OR Apache-2.0