[][src]Struct warp::filters::sse::Sse

pub struct Sse;

Extracted by the sse filter, and used to reply with stream of events.

Methods

impl Sse[src]

pub fn reply<S>(self, event_stream: S) -> impl Reply where
    S: Stream + Send + 'static,
    S::Item: ServerSentEvent,
    S::Error: StdError + Send + Sync + 'static, 
[src]

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![
        // Unnamed event with data only
        warp::sse::data("payload").boxed(),
        // Named event with ID and retry timeout
        (
            warp::sse::data("other message\nwith next line"),
            warp::sse::event("chat"),
            warp::sse::id(1),
            warp::sse::retry(Duration::from_millis(15000))
        ).boxed(),
        // Event with JSON data
        (
            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

"#
);

Trait Implementations

impl Debug for Sse[src]

Auto Trait Implementations

impl Send for Sse

impl Unpin for Sse

impl Sync for Sse

impl UnwindSafe for Sse

impl RefUnwindSafe for Sse

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> Erased for T