MessagePort object
Provides an object where messages can be sent and received through a MessageChannel.
![]() |
Members
The MessagePort object has these types of members:
Events
The MessagePort object has these events.
| Event | Description |
|---|---|
| message |
Fires when the user sends a cross-document message or a message is sent from a Worker with postMessage. |
| message |
Returns a message from the parent thread to the worker thread, or between two MessagePort objects. |
Methods
The MessagePort object has these methods.
| Method | Description |
|---|---|
| addEventListener |
Registers an event handler for the specified event type. |
| close |
Closes a MessagePort from an existing MessageChannel. |
| dispatchEvent |
Sends an event to the current element. |
| postMessage |
Sends messages between two connected MessagePorts in a MessageChannel. |
| removeEventListener |
Removes an event handler that the addEventListener method registered. |
| start |
Allows the MessagePort to begin receiving messages. |
Standards information
Remarks
Two MessagePort objects are automatically created when a MessageChannel object is created, and are returned by the port1 and port2 properties. Messages are sent from one port are received by the other, and vice versa.
The MessagePort object provides the start method to begin dispatching messages received on the port, and the close method to close and disconnect the port. The postMessage method sends messages through the port.
In Internet Explorer 10, message ports are automatically enabled when a message event is registered with the onmessage property or addEventListener method. This makes it unnecessary to explicitly call the start method under these conditions.
After posting a MessagePort object using postMessage, the MessagePort object is implicitly closed.
For more info, see MessageChannel for a working messaging demo.
See also
