This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Note: This feature is available in Web Workers.
The MessageEvent interface represents a message received by a target object.
This is used to represent messages in:
message event of EventSource).message event of WebSocket).Window.postMessage() and the message event of Window).MessagePort.postMessage() and the message event of MessagePort).Worker.postMessage(), the message event of Worker, the message event of ServiceWorkerGlobalScope, etc.)BroadcastChannel.postMessage() and the message event of BroadcastChannel).message event of RTCDataChannel).The action triggered by this event is defined in a function set as the event handler for the relevant message event.
MessageEvent()Creates a new MessageEvent.
This interface also inherits properties from its parent, Event.
MessageEvent.data Read only
The data sent by the message emitter.
MessageEvent.origin Read only
A string representing the origin of the message emitter.
MessageEvent.lastEventId Read only
A string representing a unique ID for the event.
MessageEvent.source Read only
A MessageEventSource (which can be a WindowProxy, MessagePort, or ServiceWorker object) representing the message emitter.
MessageEvent.ports Read only
An array of MessagePort objects containing all MessagePort objects sent with the message, in order.
This interface also inherits methods from its parent, Event.
initMessageEvent() Deprecated
Initializes a message event. Do not use this anymore — use the MessageEvent() constructor instead.
In our Basic shared worker example (run shared worker), we have two HTML pages, each of which uses some JavaScript to perform a calculation. The different scripts are using the same worker file to perform the calculation — they can both access it, even if their pages are running inside different windows.
The following code snippet shows creation of a SharedWorker object using the SharedWorker() constructor. Both scripts contain this:
const myWorker = new SharedWorker("worker.js");
Both scripts then access the worker through a MessagePort object created using the SharedWorker.port property. If the onmessage event is attached using addEventListener, the port is manually started using its start() method:
myWorker.port.start();
When the port is started, both scripts post messages to the worker and handle messages sent from it using port.postMessage() and port.onmessage, respectively:
[first, second].forEach((input) => {
input.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
});
myWorker.port.onmessage = (e) => {
result1.textContent = e.data;
console.log("Message received from worker");
};
Inside the worker we use the onconnect handler to connect to the same port discussed above. The ports associated with that worker are accessible in the connect event's ports property — we then use MessagePort start() method to start the port, and the onmessage handler to deal with messages sent from the main threads.
onconnect = (e) => {
const port = e.ports[0];
port.addEventListener("message", (e) => {
const workerResult = `Result: ${e.data[0] * e.data[1]}`;
port.postMessage(workerResult);
});
port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter.
};
| Specification |
|---|
| HTML> # the-messageevent-interface> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
MessageEvent |
16 | 14 | 26 | ≤12.1 | 6 | 18 | 26 | ≤12.1 | 6 | 1.0 | 4.4 | 6 |
MessageEvent |
2 | 12 | 3 | 10.6 | 4 | 18 | 4 | 11 | 3.2 | 1.0 | 4.4 | 3.2 |
data |
2 | 12 | 3 | ≤12.1 | 4 | 18 | 4 | ≤12.1 | 3.2 | 1.0 | 4.4 | 3.2 |
initMessageEvent |
2 | 12 | 3 | ≤12.1 | 4 | 18 | 4 | ≤12.1 | 3.2 | 1.0 | 4.4 | 3.2 |
lastEventId |
2 | 17 | 3 | ≤12.1 | 4 | 18 | 4 | ≤12.1 | 3.2 | 1.0 | 4.4 | 3.2 |
origin |
2 | 12 | 3 | ≤12.1 | 4 | 18 | 4 | ≤12.1 | 3.2 | 1.0 | 4.4 | 3.2 |
ports |
4 | 12 | 3 | ≤12.1 | 4 | 18 | 4 | ≤12.1 | 3.2 | 1.0 | 4.4 | 3.2 |
source |
2 | 12 | 3 | ≤12.1 | 4 | 18 | 4 | ≤12.1 | 3.2 | 1.0 | 4.4 | 3.2 |
userActivation |
72 | 79 | No | 60 | No | 72 | No | 51 | No | 11.0 | 72 | No |
ExtendableMessageEvent — similar to this interface but used in interfaces that needs to give more flexibility to authors.
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent