Expand description
§Trillium tools for server sent events
sse builds a Handler from any SseHandler, which produces a
Stream of Eventable items per connected client. Each event is
written to the client as the stream yields it.
Only requests whose Accept header names text/event-stream are answered with an event
stream; anything else — including a wildcard Accept or none at all — is passed through to
subsequent handlers untouched.
When a client goes away, the event stream is dropped promptly on every protocol, so a
stream backed by a subscription can use Drop to unsubscribe. A client that vanishes
without signalling — a killed process, a severed network — is noticed once the transport
notices, which on HTTP/3 is QUIC’s negotiated idle timeout.
use broadcaster::BroadcastChannel;
use std::time::Duration;
use trillium::Conn;
use trillium_sse::sse;
let channel = BroadcastChannel::<String>::new();
let handler = sse(move |_: &mut Conn| channel.clone()).with_heartbeat(Duration::from_secs(15));Often, you will want this stream to be something like a channel, but the specifics of that are dependent on the event fanout characteristics of your application.
§Events
This crate implements Eventable for an Event type that you can
use in your application, for String, and for &'static str. You can
also implement Eventable for any type in your application.
In addition to data events, the stream can carry comments — messages that clients ignore,
sent periodically so that an idle stream is still producing traffic. See
Event::new_comment, or Sse::with_heartbeat to have them sent automatically whenever
the stream goes quiet.
Structs§
- Event
- Events are a concrete implementation of the
Eventabletrait. - Sse
- A
Handlerthat responds to requests with a server-sent event stream.
Traits§
- Eventable
- A trait that allows any Unpin + Send + Sync type to act as an event.
- SseHandler
- The trait that defines an event source for the
Ssehandler.