mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-27 00:00:24 +02:00
Server-Sent Events (#75)
Example usage:
```rust
use axum::{prelude::*, sse::{sse, Event, KeepAlive}};
use tokio_stream::StreamExt as _;
use futures::stream::{self, Stream};
use std::{
time::Duration,
convert::Infallible,
};
let app = route("/sse", sse(make_stream).keep_alive(KeepAlive::default()));
async fn make_stream(
// you can also put extractors here
) -> Result<impl Stream<Item = Result<Event, Infallible>>, Infallible> {
// A `Stream` that repeats an event every second
let stream = stream::repeat_with(|| Event::default().data("hi!"))
.map(Ok)
.throttle(Duration::from_secs(1));
Ok(stream)
}
```
Implementation is based on [warp's](https://github.com/seanmonstar/warp/blob/master/src/filters/sse.rs)
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
use axum::{extract::TypedHeader, prelude::*, routing::nest, service::ServiceExt, sse::Event};
|
||||
use futures::stream::{self, Stream};
|
||||
use http::StatusCode;
|
||||
use std::{convert::Infallible, net::SocketAddr, time::Duration};
|
||||
use tokio_stream::StreamExt as _;
|
||||
use tower_http::{services::ServeDir, trace::TraceLayer};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
// build our application with a route
|
||||
let app = nest(
|
||||
"/",
|
||||
axum::service::get(
|
||||
ServeDir::new("examples/sse")
|
||||
.append_index_html_on_directories(true)
|
||||
.handle_error(|error: std::io::Error| {
|
||||
Ok::<_, std::convert::Infallible>((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Unhandled interal error: {}", error),
|
||||
))
|
||||
}),
|
||||
),
|
||||
)
|
||||
.route("/sse", axum::sse::sse(make_stream))
|
||||
.layer(TraceLayer::new_for_http());
|
||||
|
||||
// run it
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||
tracing::debug!("listening on {}", addr);
|
||||
hyper::Server::bind(&addr)
|
||||
.serve(app.into_make_service())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn make_stream(
|
||||
// sse handlers can also use extractors
|
||||
TypedHeader(user_agent): TypedHeader<headers::UserAgent>,
|
||||
) -> Result<impl Stream<Item = Result<Event, Infallible>>, Infallible> {
|
||||
println!("`{}` connected", user_agent.as_str());
|
||||
|
||||
// A `Stream` that repeats an event every second
|
||||
let stream = stream::repeat_with(|| Event::default().data("hi!"))
|
||||
.map(Ok)
|
||||
.throttle(Duration::from_secs(1));
|
||||
|
||||
Ok(stream)
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<script src='script.js'></script>
|
||||
@@ -0,0 +1,5 @@
|
||||
var eventSource = new EventSource('sse');
|
||||
|
||||
eventSource.onmessage = function(event) {
|
||||
console.log('Message from server ', event.data);
|
||||
}
|
||||
Reference in New Issue
Block a user