Skip to content

Demonstrates how an axum SSE handler can receive messages from a Tokio broadcast channel

Notifications You must be signed in to change notification settings

mouton0815/axum-sse-from-channel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Serve axum SSE from a Tokio channel

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.

Running the Server

RUST_LOG=debug cargo run

Connecting a Client

curl http://localhost:3000/sse

About

Demonstrates how an axum SSE handler can receive messages from a Tokio broadcast channel

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages