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.postMessage('hello');

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

BCD tables only load in the browser

See also