Expand description
WebSocket handler scaffold — fan-out via sse::EventBus with auto
JSON encode/decode + keep-alive ping. See ws::WsHub +
ws::ws_handler.
WebSocket handler scaffold — fan-out via the SSE [EventBus].
axum gives you the upgrade primitive; this module provides the production conveniences on top:
- Fan-out: every connected client receives every message sent
to the bus, via
crate::sse::EventBusunder the hood. - Auto JSON: messages are
Serialize+Deserializetypes, serialized and decoded for you. - Keep-alive: configurable ping interval keeps connections from getting reaped by intermediaries.
- Slow-consumer handling: lagging clients receive a synthetic
Lagged(n)notification (theirrecvreturnedLagged) instead of being silently dropped — they can decide whether to resync or carry on.
§Quick start
ⓘ
use rustango::sse::EventBus;
use rustango::ws::{ws_handler, WsHub};
use axum::{Router, extract::WebSocketUpgrade, response::Response};
use serde::{Serialize, Deserialize};
#[derive(Clone, Serialize, Deserialize)]
struct Tick { value: i64 }
let bus: EventBus<Tick> = EventBus::new(100);
let hub = WsHub::new(bus);
async fn ws_route(
ws: WebSocketUpgrade,
State(hub): State<WsHub<Tick>>,
) -> Response {
ws.on_upgrade(move |socket| ws_handler(socket, hub.clone()))
}
let app = Router::new().route("/ws", get(ws_route)).with_state(hub);
// From anywhere — fire one message at every connected client:
hub.broadcast(Tick { value: 42 });Structs§
- WsConfig
- Per-connection tuning knobs.
- WsHub
- Hub that fans messages out to every connected WebSocket. Cheap to
clone — internally an
Arc<broadcast::Sender>plus config.
Functions§
- ws_
handler - One connected WebSocket. Spawn from your axum handler: