runtime.sendMessage really compatible across browsers?

According to the Browser compatibility chart for runtime.sendMessage(), this API is compatible across Chrome and Firefox. I’m not seeing that to be the case though – the Firefox version seems to return a Promise where the Chrome version seems to be essentially a void return (and needs a callback function to use it asynchronously).

Is the difference expected behavior and something that should be added as a note to the page?

1 Like

Aside from the promise thing, it works pretty much the same. I haven’t had issues with differences with Chrome v Firefox. But v Edge I find some issues, can’t remember them right now i abstracted it and forgot about it.

1 Like

That’s what I’m seeing too – the Promise difference is not insignificant though!

Here is a polyfill to get the promise structure in Chrome too - https://github.com/mozilla/webextension-polyfill/

The promise way is preferred because it works so awesomely with async/await.

1 Like

Notice also that you can use callbacks in order to be compatible with every browsers.


//Promise --> works only in Firefox
browser.runtime.sendMessage(message).then(function(response){
   console.log(response);
});

is equivalent to


//Callback --> works in Firefox, Chrome and Opera
var browser = browser || chrome;
browser.runtime.sendMessage(message,function(response){
   console.log(response);
});