runtime.sendMessage()

Sends a single message to event listeners within your extension or a different extension.

This is like runtime.connect but only sends a single message, with an optional response.

If sending to your extension, the runtime.onMessage event will be fired in each page. If sending to a different extension,  runtime.onMessageExternal will be fired.

Note that extensions cannot send messages to content scripts using this method. To send messages to content scripts, use tabs.sendMessage.

Syntax

browser.runtime.sendMessage(
  extensionId,             // optional string
  message,                 // any
  options,                 // optional object
  function(response) {...} // optional function
)

Parameters

extensionIdOptional
string. The ID of the extension to send the message to. If omitted, the message will be sent to your own extension.
message
any.
optionsOptional
object.
includeTlsChannelIdOptional
boolean. Whether the TLS channel ID will be passed into onMessageExternal for processes that are listening for the connection event.
responseCallbackOptional
function. The function is passed the following arguments:
response
any. The JSON response object sent by the handler of the message. If an error occurs while connecting to the extension, the callback will be called with no arguments and runtime.lastError will be set to the error message.

Browser compatibility

EdgeFirefoxChromeOpera
Basic support?45.0Yes33
Firefox
Basic support48.0

Examples

Example add-ons

Acknowledgements

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

Document Tags and Contributors

 Contributors to this page: wbamberg
 Last updated by: wbamberg,