Skip to main content

Module sse

Module sse 

Source
Expand description

Broadcast event bus — fan-out for SSE / WebSocket / signal-driven push. See sse::EventBus. Broadcast event bus — fan one message out to every connected subscriber. The foundation for Server-Sent Events (SSE) and other real-time push patterns.

Wraps tokio::sync::broadcast with rustango-shape conveniences. For the SSE wire format, pair this with axum::response::sse::Sse and the futures-util crate in your handler — the bus stays transport-agnostic so you can also use it for WebSocket fan-out.

§Quick start

use rustango::sse::EventBus;
use serde::{Serialize, Deserialize};

#[derive(Clone, Serialize, Deserialize)]
struct Notification { message: String }

// At startup
let bus: EventBus<Notification> = EventBus::new(100);

// From any handler / signal — fan-out is fire-and-forget
bus.send(Notification { message: "Welcome".into() });

// SSE endpoint (in your handler):
//
//   let mut rx = bus.subscribe();
//   let stream = async_stream::stream! {
//       while let Ok(event) = rx.recv().await {
//           let json = serde_json::to_string(&event).unwrap_or_default();
//           yield Ok::<_, std::convert::Infallible>(
//               axum::response::sse::Event::default().data(json)
//           );
//       }
//   };
//   axum::response::sse::Sse::new(stream)
//       .keep_alive(KeepAlive::new())

Add async-stream = "0.3" and futures = "0.3" to your project Cargo.toml when you wire up the SSE handler — those crates aren’t pulled into rustango itself to keep the dep tree small.

Structs§

EventBus
Broadcast bus — fan one message out to every connected subscriber.