Skip to main content

ws_kit/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2#![forbid(unsafe_code)]
3#![deny(missing_docs)]
4
5//! # ws-kit
6//!
7//! Generic authenticated typed WebSocket toolkit.
8//!
9//! Provides:
10//! - [`config::WsConfig`] — heartbeat, broadcast capacity, connection limits with builder.
11//! - [`hub::BroadcastHub`] — typed `broadcast::Sender<T>` wrapper with connection counting, generic over `T: Clone + Serialize`.
12//! - [`room::RoomManager`] / [`room::Room`] — DashMap-backed room isolation with participant tracking.
13//! - [`extractor::TokenExtractor`] — configurable token extraction (`Authorization: Bearer`, `?token=`, `?access_token=`, `Cookie`).
14//! - [`codec::Codec`] / [`codec::JsonCodec`] — JSON encode/decode via serde.
15//!
16//! ## Quick start
17//!
18//! ```rust
19//! use ws_kit::{config::WsConfig, hub::BroadcastHub, room::RoomManager, codec::Codec};
20//! use serde::{Serialize, Deserialize};
21//!
22//! #[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
23//! struct ChatMsg { user: String, body: String }
24//!
25//! # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
26//! let hub = BroadcastHub::<ChatMsg>::new(1024);
27//! let mut rx = hub.subscribe();
28//! hub.broadcast(ChatMsg { user: "alice".into(), body: "hi".into() }).unwrap();
29//! let msg = rx.recv().await.unwrap();
30//! assert_eq!(msg.user, "alice");
31//! # });
32//! ```
33//!
34//! ## Features
35//!
36//! - `std` (default): enables `std` support for `thiserror`.
37//! - `axum` (default): enables `axum` WebSocket and `tower` deps, and `TokenExtractor::extract_token(&Parts, _)`
38//! - `tracing`: enables tracing instrumentation.
39
40pub mod codec;
41pub mod config;
42pub mod error;
43pub mod extractor;
44pub mod hub;
45pub mod room;
46
47// Re-exports for ergonomic use
48pub use codec::{Codec, JsonCodec};
49pub use config::{WsConfig, WsConfigBuilder};
50pub use error::WsError;
51pub use extractor::{TokenExtractor, TokenSourceKind, WsAuthError};
52pub use hub::BroadcastHub;
53pub use room::{Room, RoomManager};