Search code examples
browsercommunicationbackendnpapigecko

How to realize communication between browser and backend?


I have a backend software that needs to be able to communicate with a gecko-based web browser (and vice-versa). What is the best way to realize this? Since HTTP is rather one-way (with the exception of e.g. reverse AJAX which I consider to be quite "hacky") I am wondering how to do this.

Would creating an NPAPI-based plugin be an option? Based on the data exchanged between the browser and backend, the browser needs to manipulate the DOM of a webpage. The manipulations need to be quite dynamic and communication speed is an important requirement.

I am glad for any help pointing me in the right direction or providing useful resources that might be worth reading!


Solution

  • Writing browser plugins isn't quite trivial, if you can use alternatives like WebSockets (or their emulations like web-socket-js, see here and here for more details).

    Only if such alternatives don't give you enough control because of special requirements should you consider writing a browser plugin.
    With it you would get the full benefits of native code (high control over whatever API you choose) but also the problems that come with it:

    • you have to start to worry about privileges
    • bugs can crash the whole browser
    • you might have to handle behavioral differences between platforms and browsers
    • you have to worry about distribution on multiple platforms
    • ...

    If you need the higher level of control for some reason you could

    • implement the connection handling of your choice in the plugin
    • let the JavaScript initiate connections and send data
    • let the JavaScript register handlers for incoming data etc.
    • on incoming data call those handlers and pass them the data

    To get started with NPAPI plugins see here, to support IE too you'd have to write a content extension. Finally i would advise to take a look at FireBreath that already does much of the heavy lifting for you (hides the different APIs for IE and NPAPI, gives you a higher level API, fixes for browser bugs included, ...).