Module warp::filters::sse

source ·
Expand description

Server-Sent Events (SSE)

§Example


use std::time::Duration;
use std::convert::Infallible;
use warp::{Filter, sse::Event};
use futures_util::{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§

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

Functions§

  • Keeps event source connection alive when no events sent over a some time.
  • Gets the optional last event id from request. Typically this identifier represented as number or string.
  • Server-sent events reply