Expedite Performance of Node.js for CPU intensive applications like IoT
If you’re wondering why performance is important. Here’s why.
In the fast-moving world, everyone likes it right! Of course, the tiny devices make our life simpler with the fastest internet connectivity and communications. Horses are the sign of high power that we all know, that way any of the engines whether it is hardware or software engines should be powerful enough to do high-performance tasks which leads to fast-paced connectivity and device communications.
As engineers, we concentrate on each of the tiny or micro-level tasks which finally builds out a bigger system and larger outcome.
The bits and bytes of data should be processed in a high-performance fashion to achieve high efficient software applications. IoT is one such high performance requiring area where the hardware events are captured and the data is transmitted over the internet for decision making and visualization. So, it is highly important to have the device for visual communications in a high-performance fashion.
Nodejs is known for its speed of processing requests, that’s one of the reasons why Nodejs is selected as Technology for Web servers and IoT based applications.
Nexus of C++ and Nodejs[Addons] for peak performance:
Native Addons are binary files compiled from C or C++ programming that can be imported to Node Js applications as like the famous “require ‘’ npm packages. Any C++ program that is compiled using a standard library and created as a Dynamically linked library(DLL) or Shared Object(SO) can be used to work as an Addon along with Nodejs. The DLL libraries created should be exported as ``.node” file in order to be used along with the Nodejs application[Using “require’’ function]. Nodejs can load the Native Addons dynamically,
There three ways in which we can interface C++ libraries into Nodejs,
- V8, Libuv along with Node.js libraries
The third option is a bit complicated and requires a complete understanding of libuv, where the Event loop mechanism of Node js is implemented.
How to integrate C++ and Nodejs:
Let's look at the N-API, as its name depicts it is an API for building the Native Addons. This is also called Application Binary Interface(ABI), ABI based Native libraries can run even after the upgrade of Nodejs versions without any recompilations. So, it is a compiled version of API’s.
When a compiled DLL or SO library(Native Addon) is loaded into the Nodejs runtime, it communicates with Nodejs using the ABI provided by Nodejs to access or register values and perform tasks. ABI takes care of accessing the Libuv library to create a separate thread and provide a callback when the task is completed.
There are few header files provided by the Nodejs[files with .h extension] and they have the declarations of the APIs provided by the Nodejs. As you might be aware any C or C++ program can import header files [.h extension files] using its preprocessor directive[#include].
N-API or ABI generally exposes header files that can be used in Nodejs files to interact with the C++ code. Those header files will be an abstract of the actual implementation of internal libraries of Nodejs. “Node_api.h’’ is one such film that is exposed already by the N-API. It also complies with the newer version of Nodejs as well and it is written in C language, hence it can be used for both C and C++ programs
Let’s make the code for working with Native Addons,
It is necessary to install the prerequisite/supporting software. Below are the lists of such software.
- C and C++ compilers installed on the system
- Python installed in the system
- An npm module named “node-gyp” has to be installed, probably a global installation will be helpful. This will provide a cli to generate some base code.
Once the prerequisites are installed and ready, we can start creating a node project.
Lets initialize a node project by running “npm init -y” command which creates the “package.json ” file.
Next is to install a dependency module named “node-addon-api”. It will provide a header file “napi.h”. The “napi.h” file will have the necessary API’s that will be useful to communicate with Nodejs from C++ and vice-versa.
>npm install -S node-addon-api
Next is to create a “binding.gyp” file in the project root directory and put in the default content for a binding.gyp file. We can have the following content by default:
Node-gyp needs to understand the configurations necessary for setting up the build script, “binding.gyp” is the configuration file where we can provide the details/settings necessary for creating a build script for the C++ sources. Based on the configurations in the bindig.gyp file the C++ sources are compiled. Sources tag can hold all the C++ source file path,
Now we are ready to create C++ programs which can be used as an add-on for Nodejs projects.
Create an “src” folder where we can have the C++ source files.
Just for the example I have created three C++ files as follows,
As everyone understands, the C++ source will have a “.h” file which will have all the function declarations and the “.cpp” file will have the definition of the declared functions in the “.h” files.
Here we could also see the “index.cpp” file, which is really important, this file is the abstract interface for the cpp application. We also need to import the napi library[napi.h] provided by Node into the ‘index.cpp’ file, so that all the utility functions can be used in C++ code.
Napi.h will have a init function declaration that has to be implemented in the “index.cpp” file. Init function is the mapping function for C++ sources.
Now we have the complete C++ source and the corresponding mappings are ready, let’s build the C++ module,
Inorder to build the C++ sources we are going to use the “node-gyp” utility,
The first step is to configure the node-gyp file
This command will generate the Makefile necessary to build the source code,
The next step is to build the source code,
You could see in the above pic, the “.node” file got created and it is the actual module created as CPP Addon. Here the Addon outcome is “abiSample.node”. Now we can use this(require it) in any of the Nodejs code as like any other npm library.
Now let’s test it using an index.js nodejs source.
In the index.js file, we can add the C++ addon module and we run the Nodejs code and check if it is working,
Let's run the Nodejs code as “node index.js”
And the actual C++ “sayHellouser” function will be getting called:
That’s all!. Enjoy the power of Nodejs with Native addons.
It is extremely important to communicate from devices to Visuals in this IoT enabled world. Thus the C++ and Nodejs collective performance are helping us achieve faster and faster devices to visual connections.
Author: Kantharuban Sinniah