Skip to content
On this page

useBroadcastChannel

BroadcastChannel.

Parameters

typescript
import { useBroadcastChannel } from '@elonehoo/pistachio'

const scroll = useBroadcastChannel(name, onBeforeClose?)
import { useBroadcastChannel } from '@elonehoo/pistachio'

const scroll = useBroadcastChannel(name, onBeforeClose?)
ParametersTypeRequiredDefaultDescription
nameStringtrueName of the channel to be created
onBeforeCloseFuncionfalsePassed function will be called before the broadcastChannel closes

State

The useBroadcastChannel function exposes the following reactive state:

typescript
import { useBroadcastChannel } from '@elonehoo/pistachio'

const {
  supported,
  data,
  messageEvent,
  errorEvent,
  errored,
  isClosed
} = useBroadcastChannel()
import { useBroadcastChannel } from '@elonehoo/pistachio'

const {
  supported,
  data,
  messageEvent,
  errorEvent,
  errored,
  isClosed
} = useBroadcastChannel()
StateTypeDescription
supportedbooleanreturn true if the browser supports it
dataRef<T>last message data
messageEventRef<MessageEvent>Last event received
errorEventRef<MessageError>Last error Event (opens new window)received
erroredRef<boolean>true if received an invalid event
isClosedRef<boolean>true if the broadcastChannel is closed

Methods

The useBroadcastChannel function exposes the following methods:

typescript
import { useBroadcastChannel } from '@elonehoo/pistachio'

const { send, close, addListener } = useBroadcastChannel()
import { useBroadcastChannel } from '@elonehoo/pistachio'

const { send, close, addListener } = useBroadcastChannel()
SignatureDescription
send(data)send data
closecloses BroadcastChannel
addListener(cb, options?)Add new message listener

Example

Supported: false

To test please open 2 or more tabs, press send and all the other tabs should show the message

vue
<script setup lang="ts">
import { ref } from 'vue'
import { useBroadcastChannel } from '@elonehoo/pistachio'

const { supported, data, send } = useBroadcastChannel('composable-example')
const message = ref('')
const submitMessage = () => {
  send(message.value)
  message.value = ''
}
</script>

<template>
  <div>
    <p>
      Supported: <b>{{ supported }}</b>
    </p>
    <p>
      To test please open 2 or more tabs, press send and all the other tabs
      should show the message
    </p>

    <p v-if="data">
      received: {{ data }}
    </p>

    <div>
      <input
        v-model="message"
        placeholder="Write a message"
        @keydown.enter="submitMessage"
      >
      <button @click="submitMessage">
        send
      </button>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { useBroadcastChannel } from '@elonehoo/pistachio'

const { supported, data, send } = useBroadcastChannel('composable-example')
const message = ref('')
const submitMessage = () => {
  send(message.value)
  message.value = ''
}
</script>

<template>
  <div>
    <p>
      Supported: <b>{{ supported }}</b>
    </p>
    <p>
      To test please open 2 or more tabs, press send and all the other tabs
      should show the message
    </p>

    <p v-if="data">
      received: {{ data }}
    </p>

    <div>
      <input
        v-model="message"
        placeholder="Write a message"
        @keydown.enter="submitMessage"
      >
      <button @click="submitMessage">
        send
      </button>
    </div>
  </div>
</template>

Released under the MIT License.