Worker: message event

The message event is fired on a Worker object when the worker's parent receives a message from its worker (i.e. when the worker sends a message using DedicatedWorkerGlobalScope.postMessage()).

Bubbles No
Cancelable No
Interface MessageEvent
Event handler property onmessage

Examples

This code creates a new worker and listens to messages from it using addEventListener():

const worker = new Worker("static/scripts/worker.js");

worker.addEventListener('message', (event) => {
    console.log(`Received message from worker: ${event.data}`)
});

Alternatively, it could listen using the onmessage event handler property:

const worker = new Worker("static/scripts/worker.js");

worker.onmessage = (event) => {
    console.log(`Received message from worker: ${event.data}`)
};

The worker posts messages using self.postMessage():

// static/scripts/worker.js

self.postMessage('I\'m alive!');

Specifications

Specification Status
HTML Living Standard Living Standard

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
message eventChrome Full support 4Edge Full support 12Firefox Full support 3.5IE Full support 10Opera Full support 10.6Safari Full support 4WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11.5Safari iOS Full support 5.1Samsung Internet Android Full support Yes

Legend

Full support Ā 
Full support

See also