Name

MessageEvent — a message from another execution context

Inherits from

Event

Synopsis

Various APIs use message events for asynchronous communication between unrelated execution contexts. The Window, Worker, WebSocket, EventSource, and MessagePort objects all define onmessage properties for handling message events. The message associated with a message event is any JavaScript value that can be cloned as described in Structured Clones. The message is wrapped in a MessageEvent object and available on the data property. The various APIs that rely on message events also define a few additional properties in the MessageEvent object. Message events do not bubble and have no default action to cancel.

Properties

readonly any data

This property holds the message that is being delivered. data can be of any type that can be cloned with the structured clone algorithm (Structured Clones): this includes core JavaScript values including objects and arrays but not functions. Client-side values such as Document and Element nodes are not allowed, although Blobs and ArrayBuffers are.

readonly string lastEventId

For message events on an EventSource (Comet with Server-Sent Events), this field contains the lastEventId string, if any, that was sent by the server.

readonly string origin

For message events on an EventSource (Comet with Server-Sent Events) or on a Window (Cross-Origin Messaging), this property contains the origin URL of the message sender.

readonly MessagePort[] ports

For message events on a Window ...

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.