Skip to main content

Broadcaster

Struct Broadcaster 

Source
pub struct Broadcaster<K, T>
where K: Hash + Eq + Clone + Send + Sync + 'static, T: Clone + Send + Sync + 'static,
{ /* private fields */ }
Expand description

Keyed broadcast channel registry for fan-out SSE delivery.

Each key maps to an independent broadcast channel. All subscribers of a key receive every message sent to that key. Register one broadcaster per domain concept (e.g., chat messages, notifications, metrics).

§Construction

use modo::sse::{Broadcaster, SseConfig};

let chat: Broadcaster<String, ChatMessage> =
    Broadcaster::new(128, SseConfig::default());
registry.add(chat);

§Channel lifecycle

  • Channels are created lazily on first subscribe()
  • Channels are auto-cleaned when the last subscriber’s stream is dropped
  • remove() forces immediate cleanup

Implementations§

Source§

impl<K, T> Broadcaster<K, T>
where K: Hash + Eq + Clone + Send + Sync + 'static, T: Clone + Send + Sync + 'static,

Source

pub fn new(buffer: usize, config: SseConfig) -> Self

Create a new broadcaster.

  • buffer — per-channel buffer size. When a subscriber falls behind by this many messages, it lags. Typical values: 64–256 for chat, 16–64 for dashboards.
  • config — SSE configuration (keep-alive interval).
Source

pub fn subscribe(&self, key: &K) -> BroadcastStream<T>

Subscribe to a keyed channel.

Creates the channel lazily on first subscription. Returns a stream of raw T values. The stream carries a cleanup closure that removes the channel entry when the last subscriber drops.

Source

pub fn send(&self, key: &K, event: T) -> usize

Send an event to all subscribers of a key.

Returns the number of receivers that got the message. Returns 0 if no subscribers exist for the key — does NOT create a channel.

Source

pub fn subscriber_count(&self, key: &K) -> usize

Number of active subscribers for a key. Returns 0 if no channel exists.

Source

pub fn remove(&self, key: &K)

Manually remove a channel and disconnect all its subscribers.

Typically not needed — channels auto-clean on last subscriber drop. Use for explicit teardown (e.g., deleting a chat room).

Source

pub fn config(&self) -> &SseConfig

Access the SSE config.

Source

pub fn channel<F, Fut>(&self, f: F) -> Response
where F: FnOnce(Sender) -> Fut + Send + 'static, Fut: Future<Output = Result<(), Error>> + Send,

Create an SSE response with an imperative sender.

Spawns the closure as a tokio task. The closure receives a super::Sender for pushing events. The task runs until:

  • The closure returns Ok(()) — stream ends cleanly
  • The closure returns Err(e) — error is logged, stream ends
  • A tx.send() call fails — client disconnected

Panics in the closure are caught and logged.

Source

pub fn response<S>(&self, stream: S) -> Response
where S: Stream<Item = Result<Event, Error>> + Send + 'static,

Wrap an event stream into an SSE HTTP response.

Applies keep-alive comments at the configured interval and sets the X-Accel-Buffering: no header for nginx compatibility.

Trait Implementations§

Source§

impl<K, T> Clone for Broadcaster<K, T>
where K: Hash + Eq + Clone + Send + Sync + 'static, T: Clone + Send + Sync + 'static,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<K, T> Freeze for Broadcaster<K, T>

§

impl<K, T> RefUnwindSafe for Broadcaster<K, T>

§

impl<K, T> Send for Broadcaster<K, T>

§

impl<K, T> Sync for Broadcaster<K, T>

§

impl<K, T> Unpin for Broadcaster<K, T>

§

impl<K, T> UnsafeUnpin for Broadcaster<K, T>

§

impl<K, T> UnwindSafe for Broadcaster<K, T>

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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more