pub struct Broadcast { /* private fields */ }Expand description
A broadcast channel for sending messages to multiple WebSocket clients
This is useful for implementing pub/sub patterns, chat rooms, or any scenario where you need to send the same message to multiple clients.
§Example
ⓘ
use rustapi_ws::{Broadcast, Message};
use std::sync::Arc;
let broadcast = Arc::new(Broadcast::new());
// Subscribe to receive messages
let mut rx = broadcast.subscribe();
// Send a message to all subscribers
broadcast.send(Message::text("Hello everyone!"));
// Receive the message
let msg = rx.recv().await.unwrap();Implementations§
Source§impl Broadcast
impl Broadcast
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a new broadcast channel with specified capacity
Sourcepub fn send(&self, msg: Message) -> usize
pub fn send(&self, msg: Message) -> usize
Send a message to all subscribers
Returns the number of receivers that received the message. Returns 0 if there are no active subscribers.
Sourcepub fn send_text(&self, text: impl Into<String>) -> usize
pub fn send_text(&self, text: impl Into<String>) -> usize
Send a text message to all subscribers
Sourcepub fn send_json<T: Serialize>(
&self,
value: &T,
) -> Result<usize, WebSocketError>
pub fn send_json<T: Serialize>( &self, value: &T, ) -> Result<usize, WebSocketError>
Send a JSON message to all subscribers
Sourcepub fn subscriber_count(&self) -> usize
pub fn subscriber_count(&self) -> usize
Get the current number of subscribers
Sourcepub fn has_subscribers(&self) -> bool
pub fn has_subscribers(&self) -> bool
Check if there are any active subscribers
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Broadcast
impl !RefUnwindSafe for Broadcast
impl Send for Broadcast
impl Sync for Broadcast
impl Unpin for Broadcast
impl !UnwindSafe for Broadcast
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more