DedicatedWorkerGlobalScope: message event
The message
event is fired on a DedicatedWorkerGlobalScope
object when the worker receives a message from its parent (i.e. when the parent sends a message using Worker.postMessage()
).
Bubbles | No |
---|---|
Cancelable | No |
Interface | MessageEvent |
Event handler property | onmessage |
Examples
This code creates a new worker and sends it a message using Worker.postMessage()
:
const worker = new Worker("static/scripts/worker.js"); worker.addEventListener('message', (event) => { console.log(`Received message from worker: ${event.data}`) });
The worker can listen for this message using addEventListener()
:
// inside static/scripts/worker.js self.addEventListener('message', (event) => { console.log(`Received message from parent: ${event.data}`); });
Alternatively, it could listen using the onmessage
event handler property:
// static/scripts/worker.js self.onmessage = (event) => { console.log(`Received message from parent: ${event.data}`); };
Specifications
Specification |
---|
HTML Standard (HTML) # event-message |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
message_event |
4 |
12 |
3.5 |
10 |
10.6 |
4 |
≤37 |
Yes |
4 |
11.5 |
5 |
Yes |
See also
- Related events:
messageerror
. -
Worker.postMessage()
.
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope/message_event