37 lines
1.3 KiB
Markdown
37 lines
1.3 KiB
Markdown
# corex-event
|
|||
|
|
|
||
|
|
A unified events & message bus abstraction so component-to-component (or
|
||
|
|
service-to-service) communication isn't locked to one transport.
|
||
|
|
|
||
|
|
- **In-memory dispatcher** (default) — `tokio::sync::broadcast`-backed
|
||
|
|
pub/sub for communication inside a single process (monolith).
|
||
|
|
- **Distributed broker backends** — RabbitMQ (`amqp`) and NATS (`nats`)
|
||
|
|
behind the same `EventBus` trait, so swapping the backend doesn't change
|
||
|
|
handler code.
|
||
|
|
- **Typed convenience** — `TypedEventBus` layers JSON encoding on top.
|
||
|
|
|
||
|
|
## Features
|
||
|
|
|
||
|
|
- `mem` (default) — in-memory dispatcher.
|
||
|
|
- `amqp` — RabbitMQ via `lapin` (pure Rust AMQP 0.9.1 client).
|
||
|
|
- `nats` — NATS via `async-nats`.
|
||
|
|
|
||
|
|
## Usage
|
||
|
|
|
||
|
|
```rust
|
||
|
|
use corex_event::{InMemoryEventBus, TypedEventBus};
|
||
|
|
use serde::{Deserialize, Serialize};
|
||
|
|
|
||
|
|
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
||
|
|
struct OrderCreated { id: u64 }
|
||
|
|
|
||
|
|
let bus = TypedEventBus::new(InMemoryEventBus::default());
|
||
|
|
let mut sub = bus.subscribe::<OrderCreated>("orders").await?;
|
||
|
|
bus.publish("orders", &OrderCreated { id: 42 }).await?;
|
||
|
|
let event = sub.recv().await?;
|
||
|
|
```
|
||
|
|
|
||
|
|
Swap `InMemoryEventBus::default()` for `AmqpEventBus::connect(url, "exchange").await?`
|
||
|
|
or `NatsEventBus::connect(url).await?` to move to a distributed broker without
|
||
|
|
touching handler code.
|