Interface: BroadcastChannel
Defined in: types/desktop-engine.d.ts:5140
Messages to other content running at the same time, by channel name (API 6). As on the web, a message goes to every other open channel of the same name and scope, in this content and in the others the scope reaches. It never comes back to the channel that posted it, nor to one this content opens after posting it. It's a copy (see structuredClone, without transferring), delivered in a task of its own. Nothing is kept: content that isn't running or has no channel open doesn't get it. Messages from one channel arrive in the order they were posted. Content that's paused (e.g. while a full screen app covers its wallpaper) still gets them right away.
const channel = new BroadcastChannel('scene'); // the instances of this package
channel.onmessage = (event) => applyScene(event.data);
channel.postMessage({ scene: 'night' });
const focus = new BroadcastChannel('focus', { scope: 'namespace' }); // every package of this creatorOnly a channel with a listener for messages (onmessage or onmessageerror, or a "message" or "messageerror" listener added with its addEventListener) gets them. As in browsers, one without a listener can be collected once the content no longer refers to it, which closes it; one with a listener keeps receiving until close().
Limits: a message takes at most 1 MB, counting 2 bytes for each character of its encoded form (a JSON-like tree: numbers' digits, keys and array indexes, quotes, punctuation and escapes included), plus the bytes of its ArrayBuffers (a typed array's whole buffer, once), Blobs and ImageData. Send many numbers in a typed array: 30,000 numbers like i / 7 take about 1.4 MB in an array, 0.23 MB in a Float64Array. Content posts at most 240 messages and 16 MB a second over all its channels; postMessage throws past them. When content can't keep up with what it's sent (its thread is busy for long), what comes for it while 1024 messages or 64 MB wait is dropped.
Extends
Properties
name
readonly name: string;Defined in: types/desktop-engine.d.ts:5142
The name it was made with
onmessage
onmessage: ((this, event) => void) | null;Defined in: types/desktop-engine.d.ts:5146
A message came: a copy of what was posted. Setting it (or adding a "message" listener) makes the channel receive
onmessageerror
onmessageerror: ((this, event) => void) | null;Defined in: types/desktop-engine.d.ts:5148
A message came that can't be copied here
scope
readonly scope: BroadcastChannelScope;Defined in: types/desktop-engine.d.ts:5144
Who it reaches (browsers' BroadcastChannel has no scope: it reaches the same origin)
Methods
addEventListener()
addEventListener(
type,
listener,
options?
): void;Defined in: types/desktop-engine.d.ts:5370
A boolean options (capture) is ignored
Parameters
| Parameter | Type |
|---|---|
type | string |
listener | | EventListenerOrEventListenerObject | null |
options? | | boolean | AddEventListenerOptions |
Returns
void
Inherited from
close()
close(): void;Defined in: types/desktop-engine.d.ts:5159
Stops receiving; posting throws afterwards. A channel with a listener for messages keeps receiving until it's closed, so close the ones you're done with
Returns
void
dispatchEvent()
dispatchEvent(event): boolean;Defined in: types/desktop-engine.d.ts:5373
Returns false when a listener called preventDefault() on a cancelable event
Parameters
| Parameter | Type |
|---|---|
event | Event |
Returns
boolean
Inherited from
postMessage()
postMessage(message): void;Defined in: types/desktop-engine.d.ts:5154
Sends a copy of the message. Throws a "DataCloneError" DOMException when it can't be copied (see structuredClone) or is bigger than 1 MB, a "QuotaExceededError" one when the content posts too many, and an "InvalidStateError" one once the channel is closed
Parameters
| Parameter | Type |
|---|---|
message | any |
Returns
void
removeEventListener()
removeEventListener(type, listener): void;Defined in: types/desktop-engine.d.ts:5371
Parameters
| Parameter | Type |
|---|---|
type | string |
listener | | EventListenerOrEventListenerObject | null |
Returns
void
