1
0
Fork 0
mirror of https://github.com/tokio-rs/axum.git synced 2025-03-28 01:10:38 +01:00
axum/examples
David Pedersen 6d787665d6
Server-Sent Events ()
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)
2021-08-01 21:49:17 +02:00
..
self_signed_certs Add TLS example (with rustls) () 2021-08-01 08:32:47 +02:00
sse Server-Sent Events () 2021-08-01 21:49:17 +02:00
templates Add HTML template example () 2021-06-13 13:58:12 +02:00
websocket Add support for websockets () 2021-06-12 20:50:30 +02:00
error_handling_and_dependency_injection.rs Rename to axum () 2021-07-09 21:36:14 +02:00
form.rs Rename to axum () 2021-07-09 21:36:14 +02:00
hello_world.rs Rename to axum () 2021-07-09 21:36:14 +02:00
key_value_store.rs Docs improvements () 2021-07-22 15:00:33 +02:00
multipart_form.rs Add Multipart extractor for consuming multipart/form-data requests () 2021-07-14 16:53:37 +02:00
README.md Add sessions and cookies examples () 2021-08-01 09:15:44 +02:00
sessions.rs Add sessions and cookies examples () 2021-08-01 09:15:44 +02:00
sse.rs Server-Sent Events () 2021-08-01 21:49:17 +02:00
static_file_server.rs Misc clean up 2021-07-22 21:12:29 +02:00
templates.rs Rename to axum () 2021-07-09 21:36:14 +02:00
testing.rs Add testing example with real HTTP server 2021-07-22 21:12:40 +02:00
tls_rustls.rs Clean up TLS example 2021-08-01 13:48:39 +02:00
todos.rs Add todos example () 2021-07-22 15:38:32 +02:00
tokio_postgres.rs Rename to axum () 2021-07-09 21:36:14 +02:00
unix_domain_socket.rs Add extractor for remote connection info () 2021-07-31 21:36:30 +02:00
versioning.rs Make extractors easier to write () 2021-07-22 13:23:50 +02:00
websocket.rs Make websocket handlers support extractors () 2021-07-30 15:19:53 +02:00

Examples