<!--
{
  "availability" : [
    "iOS: 18.4.0 -",
    "iPadOS: 18.4.0 -",
    "macCatalyst: 18.4.0 -",
    "macOS: 15.4.0 -",
    "visionOS: 2.4.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "WebKit",
  "identifier" : "/documentation/WebKit/WKWebExtension/MessagePort/sendMessage(_:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "WebKit"
    ],
    "preciseIdentifier" : "c:objc(cs)WKWebExtensionMessagePort(im)sendMessage:completionHandler:"
  },
  "title" : "sendMessage(_:completionHandler:)"
}
-->

# sendMessage(_:completionHandler:)

Sends a message to the connected web extension.

```
func sendMessage(_ message: Any?, completionHandler: (((any Error)?) -> Void)? = nil)
```

## Parameters

`message`

The JSON-serializable message to be sent.

`completionHandler`

An optional block to be invoked after the message is sent, taking an optional error.

## Discussion> Note: The message must be JSON-serializable according to <doc://com.apple.documentation/documentation/Foundation/JSONSerialization>.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)