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

var 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.

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
sendNativeMessage
29
15
50
?
16
14
See the documentation on developer.apple.com about native messaging in Safari.
?
?
No
?
?
?

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:

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");
  var sending = browser.runtime.sendNativeMessage("ping_pong", "ping");
  sending.then(onResponse, onError);
});

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

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/sendNativeMessage