@ThreadSafe
public final class MessagePort implements AutoCloseable


Represents a single message port in a message channel.

A message port is created as part of a message channel, which consists of two entangled message ports. When one port sends a message, it is received by the other port, and vice versa. These ports can exist in separate processes. For example, a port in a Java-based Android application can send and receive messages to and from a port in a JavaScript isolate.

A message channel between the app and an isolate can be established using createMessageChannel, which returns the app-side MessagePort.

A reference to the MessagePort is maintained so long as the message channel remains alive.

In some cases, a MessagePort may become (or be created) unentangled, meaning that it has no functioning channel or counterpart port. This can happen either due to a port or channel being closed or the process or isolate on the other end being terminated.

This class is thread-safe. Messages can be posted from any thread.

Summary

Public methods

void

Closes the message port to free resources.

void

Posts a message over the channel to the other port.

Public methods

close

Added in 1.1.0-beta01
public void close()

Closes the message port to free resources.

Once the port is closed, sent and received messages are silently discarded.

postMessage

Added in 1.1.0-beta01
public void postMessage(@NonNull Message message)

Posts a message over the channel to the other port.

This method does not block on the message being processed on the receiving side and can be called from any thread.

Messages sent via a closed or dead port/channel are silently discarded.

Parameters
@NonNull Message message

The Message to send.