[][src]Module warp::filters::sse

Server-Sent Events (SSE)

Example


use std::time::Duration;
use std::convert::Infallible;
use warp::{Filter, sse::Event};
use futures::{stream::iter, Stream};

fn sse_events() -> impl Stream<Item = Result<Event, Infallible>> {
    iter(vec![
        Ok(Event::default().data("unnamed event")),
        Ok(
            Event::default().event("chat")
            .data("chat message")
        ),
        Ok(
            Event::default().id(13.to_string())
            .event("chat")
            .data("other chat message\nwith next line")
            .retry(Duration::from_millis(5000))
        )
    ])
}

let app = warp::path("push-notifications")
    .and(warp::get())
    .map(|| {
        warp::sse::reply(warp::sse::keep_alive().stream(sse_events()))
    });

Each field already is event which can be sent to client. The events with multiple fields can be created by combining fields using tuples.

See also the EventSource API, which specifies the expected behavior of Server Sent Events.

Structs

Event

Server-sent event

KeepAlive

Configure the interval between keep-alive messages, the content of each message, and the associated stream.

Functions

keep_alive

Keeps event source connection alive when no events sent over a some time.

last_event_id

Gets the optional last event id from request. Typically this identifier represented as number or string.

reply

Server-sent events reply