Extracted by the sse
filter, and used to reply with stream of events.
Server-sent events reply
This function converts stream of server events into reply.
use std::time::Duration;
use futures::stream::iter_ok;
use warp::{Filter, sse::ServerSentEvent};
#[derive(Serialize)]
struct Msg {
from: u32,
text: String,
}
let app = warp::path("sse").and(warp::sse()).map(|sse: warp::sse::Sse| {
let events = iter_ok::<_, ::std::io::Error>(vec![
warp::sse::data("payload").boxed(),
(
warp::sse::data("other message\nwith next line"),
warp::sse::event("chat"),
warp::sse::id(1),
warp::sse::retry(Duration::from_millis(15000))
).boxed(),
(
warp::sse::id(2),
warp::sse::json(Msg {
from: 2,
text: "hello".into(),
}),
).boxed(),
]);
sse.reply(events)
});
let res = warp::test::request()
.method("GET")
.header("Connection", "Keep-Alive")
.path("/sse")
.reply(&app)
.into_body();
assert_eq!(
res,
r#"data:payload
event:chat
data:other message
data:with next line
id:1
retry:15000
data:{"from":2,"text":"hello"}
id:2
"#
);
Formats the value using the given formatter. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static