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.

Software’s contribution:

Even though we have so many programming languages that are used to build highly efficient software applications, it is always the C and C++ middle-level programming languages used in IoT devices such as ADT or sensors which play a crucial role. On the other side, there are many high-level programming languages such as Java, Javascript is better for application layer programming. It’s been for years when Javascript is used only for front-end browser applications.

Nowadays Javascript is used in every component of a web application, it can be on the front-end as well as on the backend. There are many frameworks that use javascript in different forms, for example, the typescript way of usage in Angular framework, a famous front-end framework you might have come across. When the performance is considered on the front-end, it is always javascript that is considered majorly.

Javascript and Nodejs for IoT:

Javascript is powerful due to the fact that the underlying engine hosts it and helps it to run. One such engine is the V8, it is so powerful that it is helping for high-performance applications written using Javascript. The powerful engines also make the Javascript is used in the back-end applications as well. There comes the Node Js framework which is a famous back-end framework completely relying on Javascript.

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:

Still, the javascript is 10 times slower than C++ when performing CPU-intensive operations.

In order to get the peak performance in the backend applications, the best out of Nodejs and the C++ efficiency can be combined to produce top-notch applications. Nodejs runtime also provides the capability of integrating the C or C++ native libraries. This capability of Nodejs is called the Native Addon. Native Addon helps to write Node modules in native C or C++ programming languages and interfaces between Javascript and C++.

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,

  1. N-API
  2. NAN
  3. 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.

Prerequisite libraries:

  1. C and C++ compilers installed on the system
  2. Python installed in the system
  3. 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,

Hello-app.cpp,
Hello-app.h,
Index.cpp

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

>node-gyp configure

This command will generate the Makefile necessary to build the source code,

The next step is to build the source code,
>node-gyp build

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.

References:

https://nodejs.org/api/addons.html

https://medium.com/jspoint/a-simple-guide-to-load-c-c-code-into-node-js-javascript-applications-3fcccf54fd32

https://apiko.com/blog/node-js-performance-c-addons/

https://www.embeddedcomputing.com/application/misc/interfacing-iot-with-embedded-using-node-js-part-4-adc

https://medium.com/the-node-js-collection/speed-up-your-node-js-app-with-native-addons-5e76a06f4a40

https://readwrite.com/2019/05/19/what-to-expect-from-a-modern-iot-platform-in-2019

https://lynchburgbusinessmag.com/satisfaction-joy-two-critical-components-to-improving-job-performance/

Author: Kantharuban Sinniah

Your Digital Transformation partner. We are here to share knowledge on varied technologies, updates; and to stay in touch with the tech-space.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store