Skip to main content

Crate mytheclipse_event

Crate mytheclipse_event 

Source
Expand description

§mytheclipse-event

A unified events & message bus abstraction so component-to-component (or service-to-service) communication doesn’t get locked to one transport.

  • In-memory dispatcher (memory::InMemoryEventBus, feature mem, default): a tokio::sync::broadcast-backed pub/sub bus for intra-process communication.
  • Distributed broker backends: [amqp::AmqpEventBus] (RabbitMQ, feature amqp) and [nats::NatsEventBus] (NATS, feature nats) behind the same EventBus trait, so swapping the backend does not change handler code.
  • Typed convenience: [typed::TypedEventBus] layers JSON (de)serialization on top of the byte-oriented EventBus.

§Example

The in-memory bus (mem feature, on by default) carries raw bytes; the typed feature adds JSON (de)serialization on top of it.

#[cfg(feature = "mem")]
#[tokio::main]
async fn main() {
    use mytheclipse_event::{EventBus, InMemoryEventBus};
    let bus = InMemoryEventBus::default();
    let mut sub = bus.subscribe("orders").await.unwrap();
    bus.publish("orders", b"{\"id\":42}".to_vec()).await.unwrap();
    let payload = sub.recv().await.unwrap();
    assert_eq!(payload, b"{\"id\":42}");
}

#[cfg(not(feature = "mem"))]
fn main() {}

Re-exports§

pub use traits::EventBus;
pub use traits::EventError;
pub use traits::Subscription;
pub use memory::InMemoryEventBus;

Modules§

memory
In-memory pub/sub dispatcher (feature mem), backed by tokio::sync::broadcast.
prelude
The common case, in one import.
traits
The core EventBus trait and byte-oriented Subscription handle.