leptos_use

Function use_broadcast_channel

Source
pub fn use_broadcast_channel<T, C>(
    name: &str,
) -> UseBroadcastChannelReturnType<C, T, impl Fn(&T) + Clone, impl Fn() + Clone>
where C: Encoder<T, Encoded = String> + Decoder<T, Encoded = str>,
Expand description

Reactive BroadcastChannel API.

Closes a broadcast channel automatically when the component is cleaned up.

§Demo

Link to Demo

§Usage

The BroadcastChannel interface represents a named channel that any browsing context of a given origin can subscribe to. It allows communication between different documents (in different windows, tabs, frames, or iframes) of the same origin.

Messages are broadcasted via a message event fired at all BroadcastChannel objects listening to the channel.

let UseBroadcastChannelReturn {
    is_supported,
    message,
    post,
    error,
    close,
    ..
} = use_broadcast_channel::<bool, FromToStringCodec>("some-channel-name");

post(&true);

close();

Values are (en)decoded via the given codec. You can use any of the string codecs or a binary codec wrapped in Base64.

Please check the codec chapter to see what codecs are available and what feature flags they require.

// Data sent in JSON must implement Serialize, Deserialize:
#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct MyState {
    pub playing_lego: bool,
    pub everything_is_awesome: String,
}

use_broadcast_channel::<MyState, JsonSerdeCodec>("everyting-is-awesome");