# WebMessagePortCompat

Artifact: [androidx.webkit:webkit](https://developer.android.com/jetpack/androidx/releases/webkit) [View Source](https://cs.android.com/search?q=file:androidx/webkit/WebMessagePortCompat.java+class:androidx.webkit.WebMessagePortCompat) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.1.0)

*** ** * ** ***


```
@AnyThread
public abstract class WebMessagePortCompat
```

<br />

*** ** * ** ***

The Java representation of the [HTML5 message ports.](https://html.spec.whatwg.org/multipage/comms.html#messageport)

A Message port represents one endpoint of a Message Channel. In Android WebView, there is no separate Message Channel object. When a message channel is created, both ports are tangled to each other and started, and then returned in a MessagePort array, see `https://developer.android.com/reference/androidx/webkit/WebViewCompat#createWebMessageChannel(android.webkit.WebView)` for creating a message channel.

When a message port is first created or received via transfer, it does not have a WebMessageCallback to receive web messages. The messages are queued until a WebMessageCallback is set.

A message port should be closed when it is not used by the embedder application anymore. A closed port cannot be transferred or cannot be reopened to send messages. Close can be called multiple times.

When a port is transferred to JS, it cannot be used to send or receive messages at the Java side anymore. Different from HTML5 Spec, a port cannot be transferred if one of these has ever happened: i. a message callback was set, ii. a message was posted on it. A transferred port cannot be closed by the application, since the ownership is also transferred.

It is possible to transfer both ports of a channel to JS, for example for communication between subframes.

## Summary

| ### Nested types |
|---|
| `public abstract class https://developer.android.com/reference/androidx/webkit/WebMessagePortCompat.WebMessageCallbackCompat` The listener for handling MessagePort events. |

| ### Public methods |
|---|---|
| `abstract void` | `@https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.WEB_MESSAGE_PORT_CLOSE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") https://developer.android.com/reference/androidx/webkit/WebMessagePortCompat#close()()` Close the message port and free any resources associated with it. |
| `abstract void` | `@https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.WEB_MESSAGE_PORT_POST_MESSAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") https://developer.android.com/reference/androidx/webkit/WebMessagePortCompat#postMessage(androidx.webkit.WebMessageCompat)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/WebMessageCompat message)` Post a WebMessage to the entangled port. |
| `abstract void` | `@https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.WEB_MESSAGE_PORT_SET_MESSAGE_CALLBACK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") https://developer.android.com/reference/androidx/webkit/WebMessagePortCompat#setWebMessageCallback(androidx.webkit.WebMessagePortCompat.WebMessageCallbackCompat)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/WebMessagePortCompat.WebMessageCallbackCompat callback )` Sets a callback to receive message events on the main thread. |
| `abstract void` | `@https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.WEB_MESSAGE_PORT_SET_MESSAGE_CALLBACK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") https://developer.android.com/reference/androidx/webkit/WebMessagePortCompat#setWebMessageCallback(android.os.Handler,androidx.webkit.WebMessagePortCompat.WebMessageCallbackCompat)( @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Handler.html handler, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/WebMessagePortCompat.WebMessageCallbackCompat callback )` Sets a callback to receive message events on the handler that is provided by the application. |

## Public methods

### close

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.1.0)

```
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_PORT_CLOSE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract void close()
```

Close the message port and free any resources associated with it.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/androidx/webkit/WebViewFeature#WEB_MESSAGE_PORT_CLOSE()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |

### postMessage

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.1.0)

```
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_PORT_POST_MESSAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract void postMessage(@NonNull WebMessageCompat message)
```

Post a WebMessage to the entangled port.

When posting a `https://developer.android.com/reference/androidx/webkit/WebMessageCompat` with type `https://developer.android.com/reference/androidx/webkit/WebMessageCompat#TYPE_ARRAY_BUFFER()`, this method should check if `https://developer.android.com/reference/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)` returns true for `https://developer.android.com/reference/androidx/webkit/WebViewFeature#WEB_MESSAGE_ARRAY_BUFFER()`. Example:

```
if (message.getType() == WebMessageCompat.TYPE_ARRAY_BUFFER) {
    if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER) {
        // ArrayBuffer message is supported, send message here.
        port.postMessage(message);
    }
}
```

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/WebMessageCompat message` | the message from Java to JS. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If message port is already transferred or closed. |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/androidx/webkit/WebViewFeature#WEB_MESSAGE_PORT_POST_MESSAGE()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |

### setWebMessageCallback

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.1.0)

```
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_PORT_SET_MESSAGE_CALLBACK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract void setWebMessageCallback(
    @NonNull WebMessagePortCompat.WebMessageCallbackCompat callback
)
```

Sets a callback to receive message events on the main thread.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/WebMessagePortCompat.WebMessageCallbackCompat callback` | the message callback. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/androidx/webkit/WebViewFeature#WEB_MESSAGE_PORT_SET_MESSAGE_CALLBACK()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |

### setWebMessageCallback

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.1.0)

```
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_PORT_SET_MESSAGE_CALLBACK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract void setWebMessageCallback(
    @Nullable Handler handler,
    @NonNull WebMessagePortCompat.WebMessageCallbackCompat callback
)
```

Sets a callback to receive message events on the handler that is provided by the application. If the handler is null the message events are received on the main thread.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Handler.html handler` | the handler to receive the message events. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/WebMessagePortCompat.WebMessageCallbackCompat callback` | the message callback. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/androidx/webkit/WebViewFeature#WEB_MESSAGE_PORT_SET_MESSAGE_CALLBACK()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |