GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • MDN
  • HTML: Markup language
  • Elements
  • Global attributes
  • Attributes
  • See all…
  • Responsive images
  • HTML cheatsheet
  • Date & time formats
  • See all…
  • SVG
  • MathML
  • XML
  • CSS: Styling language

This view

  • Original page
  • New address

GeezerVison is showing developer.mozilla.org — MessageEvent - Web APIs | MDN

MessageEvent

Baseline
Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

  • See full compatibility
  • Learn more

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:

  • Server-sent events (see the message event of EventSource).
  • Web sockets (see the message event of WebSocket).
  • Cross-document messaging (see Window.postMessage() and the message event of Window).
  • Channel messaging (see MessagePort.postMessage() and the message event of MessagePort).
  • Cross-worker/document messaging (see the above two entries, but also Worker.postMessage(), the message event of Worker, the message event of ServiceWorkerGlobalScope, etc.)
  • Broadcast channels (see BroadcastChannel.postMessage() and the message event of BroadcastChannel).
  • WebRTC data channels (see the 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.

Constructor

MessageEvent()

Creates a new MessageEvent.

Instance properties

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.

Instance methods

This interface also inherits methods from its parent, Event.

initMessageEvent()

Initializes a message event. Do not use this anymore — use the MessageEvent() constructor instead.

Examples

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:

js
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:

js
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:

js
[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.

js
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.
};

Specifications

Specification
HTML
# the-messageevent-interface

Browser compatibility

See also

  • ExtendableMessageEvent — similar to this interface but used in interfaces that needs to give more flexibility to authors.

Help improve MDN

Learn how to contribute

This page was last modified on Dec 31, 2024 by MDN contributors.

View this page on GitHub • Report a problem with this content
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.