How does Node.js work with NW.js and Electron?
This article has been excerpted from Cross-Platform Desktop Applications
Those who’ve used Node.js in the past may be curious about how it works in a hybrid desktop application environment such as NW.js or Electron. The truth is it’s not that different from how it already works in server-side applications, but there are a few items of difference, and to understand what those are and why this is the case, we’ll start by looking at the way Node.js is integrated into NW.js.
Where Node.js fits into NW.js
NW.js’ architecture consists of several components, Node.js being one of them. The way that NW.js makes use of Node.js is to provide a way to access the computer’s file system and other resources that’d otherwise be unavailable due to web browser security. It also provides a way to use many libraries through npm.
Caveats of using Node.js in NW.js
The Node.js context is accessible to all windows
Common API methods in Chromium and Node.js
You might know that both Node.js and Blink have API methods with the same name and that work in the same way (e.g. console, setTimeout, encodeURIComponent). How are these handled? In some cases, Blink’s implementation is used, and in other cases, Node.js’ implementation is used. NW.js opts to use Blink’s implementation of console, and for setTimeout, the implementation used depends on whether the file is loaded from a Node.js module or from the desktop app. This is worth keeping in mind when you’re using those functions — although they’re consistent in their implementations of inputs and outputs, there might be a slight difference in speed of execution.
How Node.js is used within Electron
Electron uses Node.js along with Chromium, but rather than combining the event loops of the two pieces of software together, it combines them through Node.js’ node bindings feature. In this way, the Chromium and Node.js components can be updated easily without the need for custom modification of the source code, nor compilation afterwards.
If you’re interested in learning more about this approach, checkout this site from GitHub’s Jessica Lord: http://jlord.us/essential-electron/#stay-in-touch