This snippet demonstrates how an axum
SSE ("Server Sent Events") handler can receive messages
from a Tokio broadcast channel.
The trick is to pass the message Sender
to the SSE handler via axum
's
Router.with_state function.
When a HTTP client connects to the SSE handler, a new Receiver
is subscribed and wrapped into a
BroadcastStream.
Note that by definition of a broadcast channel, the HTTP client will receive messages sent after the call to subscribe. In the example, messages are created by a periodic task, but any source would be fine.
RUST_LOG=debug cargo run
curl http://localhost:3000/sse