Module actix_web_lab::sse

source ·
Expand description

Semantic server-sent events (SSE) responder with a channel-like interface.

Examples

use std::{convert::Infallible, time::Duration};
use actix_web::{Responder, get};
use actix_web_lab::sse;

#[get("/from-channel")]
async fn from_channel() -> impl Responder {
    let (sender, sse_stream) = sse::channel(10);

    // note: sender will typically be spawned or handed off somewhere else
    let _ = sender.send(sse::Event::Comment("my comment".into())).await;
    let _ = sender.send(sse::Data::new("my data").event("chat_msg")).await;

    sse_stream.with_retry_duration(Duration::from_secs(10))
}

#[get("/from-stream")]
async fn from_stream() -> impl Responder {
    let event_stream = futures_util::stream::iter([
        Ok::<_, Infallible>(sse::Event::Data(sse::Data::new("foo"))),
    ]);

    sse::Sse::from_stream(event_stream)
        .with_keep_alive(Duration::from_secs(5))
}

Complete usage examples can be found in the examples directory of the source code repo.

Structs

  • Stream implementation for channel-based SSE Sender.
  • Server-sent events data message containing a data field and optional id and event fields.
  • Error returned from SseSender::send().
  • Sender half of a server-sent events stream.
  • Server-sent events (text/event-stream) responder.

Enums

Functions

  • Create server-sent events (SSE) channel pair.