BroadcastChannel: messageerror event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2022.

Note: This feature is available in Web Workers.

The messageerror event of the BroadcastChannel interface fires when a message that can't be deserialized arrives on the channel.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("messageerror", (event) => { })

onmessageerror = (event) => { }

Event type

A MessageEvent. Inherits from Event.

Event MessageEvent

Event properties

In addition to the properties listed below, properties from the parent interface, Event, are available.

data Read only

The data sent by the message emitter.

origin Read only

A string representing the origin of the message emitter.

lastEventId Read only

A string representing a unique ID for the event.

source Read only

A message event source, which is either a WindowProxy, a MessagePort, or a ServiceWorker object representing the message emitter.

ports Read only

An array of MessagePort objects representing the ports associated with the channel the message is being sent through (where appropriate, e.g., in channel messaging or when sending a message to a shared worker).

Examples

Listening for messageerror events

This code uses addEventListener() to listen for messages and errors:

js
const channel = new BroadcastChannel("example-channel");

channel.addEventListener("message", (event) => {
  received.textContent = event.data;
});

channel.addEventListener("messageerror", (event) => {
  console.error(event);
});

The same, but using the onmessage and onmessageerror event handler properties:

js
const channel = new BroadcastChannel("example-channel");

channel.onmessage = (event) => {
  received.textContent = event.data;
};

channel.onmessageerror = (event) => {
  console.log(event);
};

Attempting to share memory

A common cause of messageerror events is attempting to send a SharedArrayBuffer object, or a buffer view backed by one, across agent clusters. The following code demonstrates this.

Page A runs the following code:

js
const channel = new BroadcastChannel("hello");
channel.postMessage({ data: new SharedArrayBuffer(1024) });

Page B runs the following code:

js
const channel = new BroadcastChannel("hello");
channel.addEventListener("messageerror", (event) => {
  console.error("Message error");
});

Then page B will receive a messageerror event when it tries to deserialize the message sent from page A.

Specifications

Specification
HTML
# event-messageerror
HTML
# handler-broadcastchannel-onmessageerror

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
messageerror event
Chrome – Full support
Chrome 60 (Release date: 2017-07-25)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 57 (Release date: 2017-11-14)
footnote Full support
Opera – Full support
Opera 47 (Release date: 2017-08-09)
footnote Full support
Safari – Full support
Safari 15.4 (Release date: 2022-03-14)
footnote Full support
Chrome Android – Full support
Chrome Android 60 (Release date: 2017-08-01)
footnote Full support
Firefox for Android – Full support
Firefox for Android 57 (Release date: 2017-11-28)
footnote Full support
Opera Android – Full support
Opera Android 47 (Release date: 2018-07-23)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15.4 (Release date: 2022-03-14)
footnote Full support
Samsung Internet – Full support
Samsung Internet 8 (Release date: 2018-07-18)
footnote Full support
WebView Android – Full support
WebView Android 60 (Release date: 2017-08-01)
footnote Full support
WebView on iOS – Full support
WebView on iOS 15.4 (Release date: 2022-03-14)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – No support
Deno 1.11 (Release date: 2021-06-08)
disabled
disabled From version 1.11 users must explicitly set the --unstable-broadcast-channel runtime flag.
Node.js – Full support
Node.js 15.4 (Release date: 2020-12-09)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
User must explicitly enable this feature.

See also