Event streams
bus.stream::<E>() reads committed events as a stream — for a GraphQL subscription or a server-sent stream — with the three limits it keeps.
A listener reacts and is done. What stays open — a GraphQL subscription, a
server-sent stream — reads the same events through bus.stream::<E>(), with
no hub, no listener and nothing to import beyond the bus. posts answers its
subscription that way:
#[subscription]#[authorize(Read, PostEntity)]async fn post_published(&self) -> impl Stream<Item = Post> { self.bus .stream::<PostPublishedEvent>() .map(|event| event.post)}Stream and the StreamExt that .map comes from are futures-util’s, a
library NestRS holds public:
nest_rs::http::futures_util beside the graphql feature, or
cargo add futures-util for events alone.
The stream reads an event when a listener would run: once the emitter’s transaction has committed, and never for one that rolled back. It reads it ahead of the listeners, so a slow one holds nothing back, and it opens no unit of work: what you do with an item runs in whatever polls the stream. Three limits come with it:
- It starts when it opens. An event committed before is not read. One emitted before and committing after is read only when another reader of its type was open at the emit, so nothing should rest on it.
- It reaches this process. A reader on another replica reads nothing, so a deployment of several replicas that must tell every reader needs a backend of its own — the bus has no broker.
- A slow reader loses events. A reader more than 64 events behind the newest
of its type loses the oldest, and one
warnonnest_rs::eventssays how many. A live feed goes on; work that must see every event is a queue job.
The stream ends with the app, once it read what it was sent.
Reference
Section titled “Reference”crates/features/src/posts/graphql/resolver.rs— a subscription reading the event as a stream.
Going further
Section titled “Going further”- Events — the bus, its listeners and the events they read.
- Event dispatch — the unit of work a listener runs in, which a stream opens none of.
- GraphQL subscriptions — the subscription a stream answers.