MessagePort Class

A message port (https://html.spec.whatwg.org/multipage/web-messaging.html#message-ports): the messages posted to a port are delivered to its entangled port, through the event loop of the relevant global object of the entangled port, once its port message queue is enabled (by MessagePort.Start or by setting MessagePort.OnMessage). The entangled ports may belong to different agents (e.g. a window and a worker): the ports can be used from the threads of their agents.

public sealed class MessagePort : EventTarget

Remarks

The relevant global object of a port is the current global object when it is created (e.g. the window of the script that creates the channel); the messages sent to a port without relevant global object are not delivered. The messages are serialized by the script engine of the global object (structured clone); without script engine, only the .NET primitive values and the serializable platform objects can be sent. A port can be transferred to another realm (the transfer list of MessagePort.PostMessage, Worker.PostMessage...).

See also

Inheritance
EventTarget MessagePort

Properties

NameDescription
OnMessageGets or sets the handler of the message event; setting it enables the port message queue.
OnMessageErrorGets or sets the event handler of the messageerror event.

Gets or sets the handler of the message event; setting it enables the port message queue.

public Action<Event>? OnMessage { get; set; }

OnMessageError

MessageChannel.cs

Gets or sets the event handler of the messageerror event.

public Action<Event>? OnMessageError { get; set; }

Methods

NameDescription
Close()Closes the port: it is disentangled (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-close).
PostMessage(object?, StructuredSerializeOptions?)Posts a message to the entangled port (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage-options).
PostMessage(object?, IReadOnlyList<object>)Posts a message to the entangled port (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage).
Start()Enables the port message queue: the pending messages are delivered (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-start).

Closes the port: it is disentangled (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-close).

public void Close()

PostMessage overloads

PostMessage(object?, StructuredSerializeOptions?)

MessageChannel.cs

Posts a message to the entangled port (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage-options).

public void PostMessage(object? message, StructuredSerializeOptions? options = null)
Parameters
message object?
The message: a value of the script engine, a .NET primitive value or a serializable platform object, or null for undefined.
options StructuredSerializeOptions?
The objects to transfer.
See also

PostMessage(object?, IReadOnlyList<object>)

MessageChannel.cs

Posts a message to the entangled port (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage).

public void PostMessage(object? message, IReadOnlyList<object> transfer)
Parameters
message object?
The message: a value of the script engine, a .NET primitive value or a serializable platform object, or null for undefined.
transfer IReadOnlyList<object>
The objects to transfer (the ArrayBuffer objects of the script engine, and the message ports).
See also

Enables the port message queue: the pending messages are delivered (https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-start).

public void Start()