mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-12 00:00:15 +02:00
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)
6 lines
140 B
JavaScript
6 lines
140 B
JavaScript
var eventSource = new EventSource('sse');
|
|
|
|
eventSource.onmessage = function(event) {
|
|
console.log('Message from server ', event.data);
|
|
}
|