1. 9 Communication
    1. 9.1 The MessageEvent interface

9 Communication

The WebSocket interface used to be defined here. It is now defined in WebSockets . [WEBSOCKETS]

9.1 The MessageEvent interface

MessageEvent

Support in all current engines.

Firefox 3+ Safari 4+ Chrome 2+
Opera 10.6+ Edge 79+
Edge (Legacy) 12+ Internet Explorer 9+
Firefox Android ? Safari iOS ? Chrome Android ? WebView Android 37+ Samsung Internet ? Opera Android 11+

Messages in server-sent events , cross-document messaging , channel messaging , broadcast channels , and WebSockets use the MessageEvent interface for their message events: [WEBSOCKETS]

[Exposed=(Window,Worker,AudioWorklet)]
interface MessageEvent : Event {
  constructor(DOMString type, optional MessageEventInit eventInitDict = {});
  readonly attribute any data;
  readonly attribute USVString origin;
  readonly attribute DOMString lastEventId;
  readonly attribute MessageEventSource? source;
  readonly attribute FrozenArray<MessagePort> ports;
  undefined initMessageEvent(DOMString type, optional boolean bubbles = false, optional boolean cancelable = false, optional any data = null, optional USVString origin = "", optional DOMString lastEventId = "", optional MessageEventSource? source = null, optional sequence<MessagePort> ports = []);
};
dictionary MessageEventInit : EventInit {
  any data = null;
  USVString origin = "";
  DOMString lastEventId = "";
  MessageEventSource? source = null;
  sequence<MessagePort> ports = [];
};
typedef
(

WindowProxy

or

MessagePort

or

ServiceWorker

)

MessageEventSource

;

Each MessageEvent has an origin (an origin , a string, or null), initially null.

event . data

MessageEvent/data

Support in all current engines.

Firefox 3+ Safari 4+ Chrome 2+
Opera 12.1+ Edge 79+
Edge (Legacy) 12+ Internet Explorer 9+
Firefox Android ? Safari iOS ? Chrome Android ? WebView Android 37+ Samsung Internet ? Opera Android 12.1+

Returns the data of the message.

event . origin

MessageEvent/origin

Support in all current engines.

Firefox 3+ Safari 4+ Chrome 2+
Opera 12.1+ Edge 79+
Edge (Legacy) 12+ Internet Explorer 9+
Firefox Android ? Safari iOS ? Chrome Android ? WebView Android 37+ Samsung Internet ? Opera Android 12.1+

Returns the origin of the message, for server-sent events and cross-document messaging .

event . lastEventId

MessageEvent/lastEventId

Support in all current engines.

Firefox 3+ Safari 4+ Chrome 2+
Opera 12.1+ Edge 79+
Edge (Legacy) 17+ Internet Explorer 9+
Firefox Android ? Safari iOS ? Chrome Android ? WebView Android 37+ Samsung Internet ? Opera Android 12.1+

Returns the last event ID string , for server-sent events .

event . source

MessageEvent/source

Support in all current engines.

Firefox 3+ Safari 4+ Chrome 2+
Opera 12.1+ Edge 79+
Edge (Legacy) 12+ Internet Explorer No
Firefox Android ? Safari iOS ? Chrome Android ? WebView Android 37+ Samsung Internet ? Opera Android 12.1+

Returns the WindowProxy of the source window, for cross-document messaging , and the MessagePort being attached, in the connect event fired at SharedWorkerGlobalScope objects.

event . ports

MessageEvent/ports

Support in all current engines.

Firefox 3+ Safari 4+ Chrome 4+
Opera 12.1+ Edge 79+
Edge (Legacy) 12+ Internet Explorer 9+
Firefox Android ? Safari iOS ? Chrome Android ? WebView Android 37+ Samsung Internet ? Opera Android 12.1+

Returns the MessagePort array sent with the message, for cross-document messaging and channel messaging .

The data attribute must return the value it was initialized to. It represents the message being sent.

The origin attribute represents, in server-sent events and cross-document messaging , the origin of the document that sent the message (typically the scheme, hostname, and port of the document, but not its path or fragment ).

The origin getter steps are:

  1. If this 's origin is an origin , then return the serialization of this 's origin .

  2. If this 's origin is null, then return the empty string.

  3. Return this 's origin .

When the origin attribute is "initialized" (during MessageEvent 's constructor , for example), the initialization value is placed into the object's origin .

Objects implementing the MessageEvent interface's extract an origin steps are to return this 's origin if it is an origin ; otherwise null.

The lastEventId attribute must return the value it was initialized to. It represents, in server-sent events , the last event ID string of the event source.

The source attribute must return the value it was initialized to. It represents, in cross-document messaging , the WindowProxy of the browsing context of the Window object from which the message came; and in the connect events used by shared workers , the newly connecting MessagePort .

The ports attribute must return the value it was initialized to. It represents, in cross-document messaging and channel messaging , the MessagePort array being sent.

The initMessageEvent( type , bubbles , cancelable , data , origin , lastEventId , source , ports ) method must initialize the event in a manner analogous to the similarly-named initEvent() method. [DOM]

Various APIs (e.g., WebSocket , EventSource ) use the MessageEvent interface for their message event without using the MessagePort API.