Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useBroadcastChannel.md
1---2category: Browser3---45# useBroadcastChannel67Reactive [BroadcastChannel API](https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel).89Closes a broadcast channel automatically component unmounted.1011## Usage1213The BroadcastChannel interface represents a named channel that any browsing14context of a given origin can subscribe to. It allows communication between15different documents (in different windows, tabs, frames, or iframes) of the16same origin.1718Messages are broadcasted via a message event fired at all BroadcastChannel19objects listening to the channel.2021```ts22import { useBroadcastChannel } from '@vueuse/core'23import { shallowRef } from 'vue'2425const {26isSupported,27channel,28post,29close,30error,31isClosed,32} = useBroadcastChannel({ name: 'vueuse-demo-channel' })3334const message = shallowRef('')3536message.value = 'Hello, VueUse World!'3738// Post the message to the broadcast channel:39post(message.value)4041// Option to close the channel if you wish:42close()43```4445## Type Declarations4647```ts48export interface UseBroadcastChannelOptions extends ConfigurableWindow {49/**50* The name of the channel.51*/52name: string53}54/**55* Reactive BroadcastChannel56*57* @see https://vueuse.org/useBroadcastChannel58* @see https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel59* @param options60*61*/62export declare function useBroadcastChannel<D, P>(63options: UseBroadcastChannelOptions,64): UseBroadcastChannelReturn<D, P>65export interface UseBroadcastChannelReturn<D, P> extends Supportable {66channel: Ref<BroadcastChannel | undefined>67data: Ref<D>68post: (data: P) => void69close: () => void70error: ShallowRef<Event | null>71isClosed: ShallowRef<boolean>72}73```74