actix_sse/lib.rs
1//! Semantic server-sent events (SSE) responder
2//!
3//! # Examples
4//! ```no_run
5//! use std::{convert::Infallible, time::Duration};
6//!
7//! use actix_web::{Responder, get};
8//! use tokio_stream::wrappers::ReceiverStream;
9//!
10//! #[get("/from-channel")]
11//! async fn from_channel() -> impl Responder {
12//! let (tx, rx) = tokio::sync::mpsc::channel(10);
13//!
14//! // note: sender will typically be spawned or handed off somewhere else
15//! let _ = tx.send(actix_sse::Event::Comment("my comment".into())).await;
16//! let _ = tx
17//! .send(actix_sse::Data::new("my data").event("chat_msg").into())
18//! .await;
19//!
20//! let event_stream = ReceiverStream::new(rx);
21//! actix_sse::Sse::from_infallible_stream(event_stream).with_retry_duration(Duration::from_secs(10))
22//! }
23//!
24//! #[get("/from-stream")]
25//! async fn from_stream() -> impl Responder {
26//! let event_stream = futures_util::stream::iter([Ok::<_, Infallible>(actix_sse::Event::Data(
27//! actix_sse::Data::new("foo"),
28//! ))]);
29//!
30//! actix_sse::Sse::from_stream(event_stream).with_keep_alive(Duration::from_secs(5))
31//! }
32//! ```
33
34pub use self::data::Data;
35pub use self::event::Event;
36pub use self::sse::Sse;
37use self::stream::InfallibleStream;
38
39mod data;
40mod event;
41mod sse;
42mod stream;