runtime.sendNativeMessage()

Sends a single message from an extension to a native application.

This takes two mandatory parameters: the name of the native application and a JSON object which is the message to send it. The browser will launch the native application and deliver the message.

This is an asynchronous function that returns a Promise. The first message sent by the native application is treated as a response to the sendNativeMessage() call, and the promise will be fulfilled with this message as a parameter. Note that you can't use runtime.onMessage to get responses from the application: you must use the callback function instead.

A new instance of the application is launched for call to runtime.sendNativeMessage(). The browser will terminate the native application after getting a reply. To terminate a native application, the browser will close the pipe, give the process a few seconds to exit gracefully, and then kill it if it has not exited.

For more information, see Native messaging.

Syntax

js
let sending = browser.runtime.sendNativeMessage(
  application,             // string
  message                  // object
)

Parameters

application

string. The name of the native application. This must match the "name" property in the native application's manifest file.

message

object. A JSON object that will be sent to the native application.

Return value

A Promise. If the sender sent a response, this will be fulfilled with the response as a JSON object. Otherwise it will be fulfilled with no arguments. If an error occurs while connecting to the native application, the promise will be rejected with an error message.

Examples

Here's a background script that sends a "ping" message to the "ping_pong" app and logs the response, whenever the user clicks the browser action:

js
function onResponse(response) {
  console.log(`Received ${response}`);
}

function onError(error) {
  console.log(`Error: ${error}`);
}

/*
On a click on the browser action, send the app a message.
*/
browser.browserAction.onClicked.addListener(() => {
  console.log("Sending:  ping");
  let sending = browser.runtime.sendNativeMessage("ping_pong", "ping");
  sending.then(onResponse, onError);
});

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Firefox for Android
Safari on iOS
sendNativeMessage
Chrome – Full support
Chrome 29 (Release date: 2013-08-20)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 50 (Release date: 2016-11-15)
footnote Full support
Opera – Full support
Opera 16 (Release date: 2013-08-27)
footnote Full support
Safari – Full support
Safari 14 (Release date: 2020-09-16)
footnote
footnote See the Apple Developer Documentation for additional details.
Firefox for Android – No support
Firefox for Android
footnote
footnote Only available to privileged extensions in GeckoView 68 and later. See the GeckoView Docs for additional details.
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote
footnote See the Apple Developer Documentation for additional details.

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.

Note: This API is based on Chromium's chrome.runtime API. This documentation is derived from runtime.json in the Chromium code.