Skip to main content

BroadcastHub

Struct BroadcastHub 

Source
pub struct BroadcastHub<T>
where T: Clone + Serialize + Send + Sync + 'static,
{ /* private fields */ }
Expand description

A typed broadcast hub for WebSocket messages.

T must be Clone (required by tokio::sync::broadcast) and Serialize so that it can be encoded via crate::codec::Codec.

The hub is cheap to clone — all clones share the same underlying channel and connection counter.

§Example

use ws_kit::hub::BroadcastHub;
use serde::{Serialize, Deserialize};

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
struct Msg { text: String }

let hub = BroadcastHub::<Msg>::new(16);
let mut rx = hub.subscribe();
hub.broadcast(Msg { text: "hello".into() }).unwrap();
assert_eq!(rx.recv().await.unwrap().text, "hello");

Implementations§

Source§

impl<T> BroadcastHub<T>
where T: Clone + Serialize + Send + Sync + 'static,

Source

pub fn new(capacity: usize) -> Self

Create a new hub with the given broadcast capacity.

Source

pub fn from_config(config: &WsConfig) -> Self

Create a hub from crate::config::WsConfig.

Source

pub fn subscribe(&self) -> Receiver<T>

Subscribe to the broadcast channel.

Source

pub fn broadcast(&self, msg: T) -> Result<usize, WsError>

Broadcast a message to all subscribers.

Returns the number of receivers that received the message.

§Errors

Returns WsError::BroadcastFull if there are no active receivers. tokio::sync::broadcast does not have a full error — the buffer is a ring — but to preserve the WsError::BroadcastFull contract we surface it when send fails due to lack of receivers or lag.

Source

pub fn try_broadcast(&self, msg: T) -> usize

Try to broadcast, returning Ok(0) when there are no receivers instead of an error. Useful for fire-and-forget.

Source

pub fn connection_count(&self) -> u64

Current number of tracked connections.

Source

pub fn increment_connections(&self, max: Option<usize>) -> Result<u64, WsError>

Atomically increment the connection counter.

Returns the new count. If max is Some, returns WsError::TooManyConnections when the limit would be exceeded and does not increment.

Source

pub fn decrement_connections(&self) -> u64

Atomically decrement the connection counter (saturating).

Source

pub fn receiver_count(&self) -> usize

Number of active receivers.

Source

pub fn capacity(&self) -> usize

Channel capacity.

Source

pub fn sender(&self) -> &Sender<T>

Internal sender handle (for advanced use).

Source§

impl<T> BroadcastHub<T>
where T: Clone + Serialize + Send + Sync + 'static,

Source

pub fn with_capacity(capacity: usize) -> Self

Create hub with explicit capacity tracking (internal).

Trait Implementations§

Source§

impl<T> Clone for BroadcastHub<T>
where T: Clone + Serialize + Send + Sync + 'static,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for BroadcastHub<T>
where T: Clone + Serialize + Send + Sync + 'static,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BroadcastHub<T>
where Sender<T>: Freeze,

§

impl<T> RefUnwindSafe for BroadcastHub<T>

§

impl<T> Send for BroadcastHub<T>
where Sender<T>: Send,

§

impl<T> Sync for BroadcastHub<T>
where Sender<T>: Sync,

§

impl<T> Unpin for BroadcastHub<T>
where Sender<T>: Unpin,

§

impl<T> UnsafeUnpin for BroadcastHub<T>
where Sender<T>: UnsafeUnpin,

§

impl<T> UnwindSafe for BroadcastHub<T>
where Sender<T>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.