Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding node.js in a Firefox extension and running a server in-browser

I am trying to figure out how to embed node.js within a Firefox extension, so that I can run a persistent server process (specifically PeerServer) from within the browser, as long as the user has the extension enabled. The only thing I've been able to find online is this guide ... but I haven't been able to make those instructions work, and need to find some more resources.

1) Does anyone have any other resources besides the article I linked to above that talk about embedding node.js in Firefox extensions? Any code examples?

2) Is there some reason that it would not be possible to run a persistent server process such as PeerServer from within a Firefox extension? Are there some kind of limitations on extensions that would prevent me from being able to do that?

like image 621
jessetaylor84 Avatar asked Jan 22 '14 19:01

jessetaylor84


People also ask

CAN NodeJS run in Firefox?

Node. js is built with the V8 JavaScript engine from Google's Chrome browser, but Mozilla is transplanting Firefox's JavaScript technology in a project called SpiderNode. (The JavaScript engine in Firefox is called SpiderMonkey, and the hybrid technology used in SpiderNode is called V8Monkey.)

CAN NodeJS run in a browser?

Thanks to some creative engineers, it is now feasible to use Node. js modules in browsers, but not directly. Being able to call Node. js modules from JavaScript running in the browser has many advantages because it allows you to use Node.


1 Answers

You can just have the executable in a folder of your extension and have JS code in the extension launch that executable. Running an external executable is described in the resource you linked or here at MDN.

Example copied from MDN:

var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsIFile);
file.initWithPath("myapp.exe");

var process = Components.classes["@mozilla.org/process/util;1"]
                        .createInstance(Components.interfaces.nsIProcess);
process.init(file);

var args = ["argument1", "argument2"];
process.run(false, args, args.length);

A bit more logic is needed to find the absolute path of the user's profile to derive the path of the application to launch but it's doable.

Now if you want to interact with node from the extension you could use HTTP requests as a means of communication.

It sounds a bit strange to embed node in Firefox though when Firefox itself has a JS engine at its core. A more elegant approach would be to try to get PeerJS running directly in Firefox addon context, without node. Maybe more complicated but it should be possible. See for example this addon "Browser Server".

like image 193
Yolanda Ruiz Avatar answered Nov 05 '22 11:11

Yolanda Ruiz