SlideShare a Scribd company logo
Node.js Native AddOns from zero to hero
Nicola Del Gobbo
ROME - APRIL 13/14 2018
Developer
N-API TEAM
Who is Nicola Del Gobbo?
What is Node.js Native Add-on?
C / C++ code called from JavaScript
BRIDGE
Native environment JavaScript
From Node.js documentation
Node.js Addons are dynamically-linked shared objects, written in
C++, that can be loaded into Node.js using the require() function,
and used just as if they were an ordinary Node.js module.
They are used primarily to provide an interface between JavaScript
running in Node.js and C/C++ libraries.
How is it possible?
All the magic is behind the Node.js architecture
Why?
Performance
In general C / C++ code performs better than
JavaScript code, but it’s not always true.
Image processing (in average 6 times faster)
Video processing
CRC cyclic redundancy check (in average 125 times faster)
Compression
Scientific calculus
Algorithms that execute CPU heavy tasks
Why?
Integrate legacy application
You have the source code of an old C / C++ application
and want to expose something of its functionalities
Why?
You don’t find what fits your specific needs on npm
Sometimes completely implementing the module
in JavaScript is not the right solution
Think at libraries like:
ImageMagick
Ghostscript
FFmpeg
TensorFlow
…
Why?
Better error handling
Even if you will use an old C / C++ library you will get an
error code that explains the error that just happened
In new C / C++ library you have the exception
You don’t have to parse some string to identify if there
was an error and what kind of it
With great power comes great responsibility
Ben Parker
Problems
Fragmentation API
The API to implement native add-ons has been changed across
different version of Node.js
Most of the changes were on V8 API and ObjectWrap API
0.8 - 0.10.x - 0.12.x - 1.x - 2.x - 3.x - 4.x - 5.x - 6.x - 7.x - 8.x - 9.x
For more info http://v8docs.nodesource.com/
Problems
Need an adapter to stay compatible across different
version of Node.js
• NAN - Native Abstraction for Node.js
• API compatibility
• Strong bonded with V8 API
• You have to recompile your native add-ons switching to
different version of Node.js
Problems
End user
Mantainers
Problems
Write portable C / C++ code
Your native code must compile and run on different:
ARCHITECTURE PLATFORM COMPILER
Problems
Documentation
• C / C++ libraries that you are integrating are not well documented
• There are good references but not so much practical guide
focusing on complex concepts about native add-ons
N-API
N-API will be a game changer on the native add-on
development
• API and ABI compatibility
• Isolated from V8 (VM agnostic)
• New ES6 types
• C / C++ (only header wrapper)
• Conversion tool that helps to migrate from NAN
• Generator (helps with initial scaffolding)
• Pre-builds (node-pre-gyp - prebuildify)
How to organize your project
Example: the JavaScript part
Example: the JavaScript part
Example: the binding.gyp
Example: the C / C++ part
See the example here
https://github.com/NickNaso/conf-cd-rome-2018/tree/master/00-echo
Asynchronous code
Asynchronous code
See the example here
https://github.com/NickNaso/conf-cd-rome-2018/tree/master/01-pass-
function
Asynchronous code
Preserve reference to function we want call
Create worker threads using libuv
Handle the results
AsyncWorker
AsyncWorker
See the example here
https://github.com/NickNaso/conf-cd-rome-2018/tree/master/02-async-
worker
AsyncWorker
You cannot call JavaScript function from the Worker Thread
until this PR will be landed
• ObjectWrap is a way to expose your C++ code to JavaScript
• You have to extend ObjectWrap class that includes the plumbing to
connect JavaScript code to a C++ object
• Classes extending ObjectWrap can be instantiated from JavaScript
using the new operator, and their methods can be directly invoked
from JavaScript
• Unfortunately, the wrap part really refers to a way to group methods and
state
• It’s your responsibility write custom code to bridge each of your C++
class methods.
ObjectWrap API
Much of the Node.js core API modules are built aroud an idiomatic asynchronous
event-driven architecture in which certains kinds of objects (called emitter)
periodically emit named events that cause Function objects ("listeners") to be
called.
• Emit event from C++
• Implement a native add-on object that inherits from Event Emitter
Event Emitter
See the example here
https://github.com/NickNaso/conf-cd-rome-2018/tree/master/04-addon-
event-emitter
A stream is an abstract interface for working with streaming data in Node.js.
The stream module provides a base API that makes it easy to build objects
that implement the stream interface.
Using Transform stream to pass and get back data from a native add-on
Stream
See the example here
https://github.com/NickNaso/conf-cd-rome-2018/tree/master/05-addon-
stream
• Create simple interface between C / C++ and JavaScript
• Think carefully at JavaScript interface
• Use ObjectWrap API to return C / C++ object to JavaScript
• Validate the types on the native side
• Expose only the functionalities you need
• Don’t block the event loop, stay asynchronous: this is a
must if you will use the add-ons in high performance services
• Use Buffer to pass big quantity of data
Lessons learned
• The largest number of native add-ons is written using NAN
• N-API will be go out of experimental very soon
• Backports for 8.x
• Backports for 6.x
• Documentation for node-addon-api
• Porting preexistentes native addon-ons to N-API
• Investigate how to use prebuild with N-API
Present and future
Implement key value database
Binding to Vedis an embeddable datastore C library
Thanks!
https://github.com/NickNaso/conf-cd-rome-2018
All examples and materials are here
Don’t be afraid sometimes it’s a good thing dirty your hands
with a little of C++
nicoladelgobbo@gmail.com
@NickNaso on Twitter

More Related Content

What's hot (20)

PPTX
K8s from Zero to ~Hero~ Seasoned Beginner
Kristof Jozsa
 
PPTX
N api - node interactive 2017
Michael Dawson
 
PPTX
Mini .net conf 2020
Marco Parenzan
 
PDF
How to build a tool for operating Flink on Kubernetes
AndreaMedeghini
 
PDF
API Design in the Modern Era - Architecture Next 2020
Eran Stiller
 
PPTX
Infrastrucutre as Code
Harmeet Singh
 
PDF
Running AWS Locally
Chris Gillespie
 
PPTX
C++ on the Web: Run your big 3D game in the browser
Andre Weissflog
 
PDF
Sailing into 2018 with Kubernetes and Istio
Fernand Galiana
 
PDF
2016 07 - CloudBridge Python library (XSEDE16)
Enis Afgan
 
PPTX
Migrating .NET Application to .NET Core
Baris Ceviz
 
PPTX
DevNetCreate Workshop - build a react app - React crash course
Cisco DevNet
 
PPTX
N-API NodeSummit-2017
Arunesh Chandra
 
PDF
N api-node summit-2017-final
Michael Dawson
 
PPTX
.NET Core: a new .NET Platform
Alex Thissen
 
PDF
Building Translate on Glass
Trish Whetzel
 
PPTX
Helm at reddit: from local dev, staging, to production
Gregory Taylor
 
PDF
High Productivity Web Development Workflow
Vũ Nguyễn
 
PPTX
Gabriele Provinciali/Gabriele Folchi/Luca Postacchini - Sviluppo con piattafo...
Codemotion
 
PPTX
Net core
Damir Dobric
 
K8s from Zero to ~Hero~ Seasoned Beginner
Kristof Jozsa
 
N api - node interactive 2017
Michael Dawson
 
Mini .net conf 2020
Marco Parenzan
 
How to build a tool for operating Flink on Kubernetes
AndreaMedeghini
 
API Design in the Modern Era - Architecture Next 2020
Eran Stiller
 
Infrastrucutre as Code
Harmeet Singh
 
Running AWS Locally
Chris Gillespie
 
C++ on the Web: Run your big 3D game in the browser
Andre Weissflog
 
Sailing into 2018 with Kubernetes and Istio
Fernand Galiana
 
2016 07 - CloudBridge Python library (XSEDE16)
Enis Afgan
 
Migrating .NET Application to .NET Core
Baris Ceviz
 
DevNetCreate Workshop - build a react app - React crash course
Cisco DevNet
 
N-API NodeSummit-2017
Arunesh Chandra
 
N api-node summit-2017-final
Michael Dawson
 
.NET Core: a new .NET Platform
Alex Thissen
 
Building Translate on Glass
Trish Whetzel
 
Helm at reddit: from local dev, staging, to production
Gregory Taylor
 
High Productivity Web Development Workflow
Vũ Nguyễn
 
Gabriele Provinciali/Gabriele Folchi/Luca Postacchini - Sviluppo con piattafo...
Codemotion
 
Net core
Damir Dobric
 

Similar to Nodejs Native Add-Ons from zero to hero (20)

PPTX
Nodejs overview
Nicola Del Gobbo
 
PPTX
Node.js/io.js Native C++ Addons
Chris Barber
 
PDF
ITCamp 2017 - Raffaele Rialdi - A Deep Dive Into Bridging Node-js with .NET Core
ITCamp
 
PPTX
Node js for beginners
Arjun Sreekumar
 
PDF
Extending Node.js using C++
Kenneth Geisshirt
 
PDF
Raffaele Rialdi
CodeFest
 
KEY
node.js: Javascript's in your backend
David Padbury
 
PDF
NodeJS for Beginner
Apaichon Punopas
 
PPTX
Node.js code tracing
Geng-Dian Huang
 
PDF
Node.js introduction
Prasoon Kumar
 
PPTX
Introduction to Node.js
Vikash Singh
 
PDF
Node.js extensions in C++
Kenneth Geisshirt
 
PPT
Node
Manav Prasad
 
PDF
Nodejs a-practical-introduction-oredev
Felix Geisendörfer
 
PPTX
JS & NodeJS - An Introduction
Nirvanic Labs
 
KEY
Node.js - A practical introduction (v2)
Felix Geisendörfer
 
PPTX
Getting started with Emscripten – Transpiling C / C++ to JavaScript / HTML5
David Voyles
 
PPTX
Proposal
Constantine Priemski
 
PDF
Build App with Nodejs - YWC Workshop
Sarunyhot Suwannachoti
 
PPTX
Introduction to node.js
Arun Kumar Arjunan
 
Nodejs overview
Nicola Del Gobbo
 
Node.js/io.js Native C++ Addons
Chris Barber
 
ITCamp 2017 - Raffaele Rialdi - A Deep Dive Into Bridging Node-js with .NET Core
ITCamp
 
Node js for beginners
Arjun Sreekumar
 
Extending Node.js using C++
Kenneth Geisshirt
 
Raffaele Rialdi
CodeFest
 
node.js: Javascript's in your backend
David Padbury
 
NodeJS for Beginner
Apaichon Punopas
 
Node.js code tracing
Geng-Dian Huang
 
Node.js introduction
Prasoon Kumar
 
Introduction to Node.js
Vikash Singh
 
Node.js extensions in C++
Kenneth Geisshirt
 
Nodejs a-practical-introduction-oredev
Felix Geisendörfer
 
JS & NodeJS - An Introduction
Nirvanic Labs
 
Node.js - A practical introduction (v2)
Felix Geisendörfer
 
Getting started with Emscripten – Transpiling C / C++ to JavaScript / HTML5
David Voyles
 
Build App with Nodejs - YWC Workshop
Sarunyhot Suwannachoti
 
Introduction to node.js
Arun Kumar Arjunan
 
Ad

More from Nicola Del Gobbo (6)

PPTX
Nodejs from zero to hero
Nicola Del Gobbo
 
PPTX
Expressjs from-zero-to-hero
Nicola Del Gobbo
 
PPTX
Introduzione a Node.js
Nicola Del Gobbo
 
PPTX
Lexgenda Documenti d’archivio e nuove tecnologie
Nicola Del Gobbo
 
PPTX
Automatic generation of inspection checklist by user profiling
Nicola Del Gobbo
 
PPTX
Lexgenda
Nicola Del Gobbo
 
Nodejs from zero to hero
Nicola Del Gobbo
 
Expressjs from-zero-to-hero
Nicola Del Gobbo
 
Introduzione a Node.js
Nicola Del Gobbo
 
Lexgenda Documenti d’archivio e nuove tecnologie
Nicola Del Gobbo
 
Automatic generation of inspection checklist by user profiling
Nicola Del Gobbo
 
Ad

Recently uploaded (20)

PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 

Nodejs Native Add-Ons from zero to hero

  • 1. Node.js Native AddOns from zero to hero Nicola Del Gobbo ROME - APRIL 13/14 2018
  • 2. Developer N-API TEAM Who is Nicola Del Gobbo?
  • 3. What is Node.js Native Add-on? C / C++ code called from JavaScript BRIDGE Native environment JavaScript
  • 4. From Node.js documentation Node.js Addons are dynamically-linked shared objects, written in C++, that can be loaded into Node.js using the require() function, and used just as if they were an ordinary Node.js module. They are used primarily to provide an interface between JavaScript running in Node.js and C/C++ libraries.
  • 5. How is it possible? All the magic is behind the Node.js architecture
  • 6. Why? Performance In general C / C++ code performs better than JavaScript code, but it’s not always true. Image processing (in average 6 times faster) Video processing CRC cyclic redundancy check (in average 125 times faster) Compression Scientific calculus Algorithms that execute CPU heavy tasks
  • 7. Why? Integrate legacy application You have the source code of an old C / C++ application and want to expose something of its functionalities
  • 8. Why? You don’t find what fits your specific needs on npm Sometimes completely implementing the module in JavaScript is not the right solution Think at libraries like: ImageMagick Ghostscript FFmpeg TensorFlow …
  • 9. Why? Better error handling Even if you will use an old C / C++ library you will get an error code that explains the error that just happened In new C / C++ library you have the exception You don’t have to parse some string to identify if there was an error and what kind of it
  • 10. With great power comes great responsibility Ben Parker
  • 11. Problems Fragmentation API The API to implement native add-ons has been changed across different version of Node.js Most of the changes were on V8 API and ObjectWrap API 0.8 - 0.10.x - 0.12.x - 1.x - 2.x - 3.x - 4.x - 5.x - 6.x - 7.x - 8.x - 9.x For more info http://v8docs.nodesource.com/
  • 12. Problems Need an adapter to stay compatible across different version of Node.js • NAN - Native Abstraction for Node.js • API compatibility • Strong bonded with V8 API • You have to recompile your native add-ons switching to different version of Node.js
  • 14. Problems Write portable C / C++ code Your native code must compile and run on different: ARCHITECTURE PLATFORM COMPILER
  • 15. Problems Documentation • C / C++ libraries that you are integrating are not well documented • There are good references but not so much practical guide focusing on complex concepts about native add-ons
  • 16. N-API N-API will be a game changer on the native add-on development • API and ABI compatibility • Isolated from V8 (VM agnostic) • New ES6 types • C / C++ (only header wrapper) • Conversion tool that helps to migrate from NAN • Generator (helps with initial scaffolding) • Pre-builds (node-pre-gyp - prebuildify)
  • 17. How to organize your project
  • 21. Example: the C / C++ part
  • 22. See the example here https://github.com/NickNaso/conf-cd-rome-2018/tree/master/00-echo
  • 25. See the example here https://github.com/NickNaso/conf-cd-rome-2018/tree/master/01-pass- function
  • 26. Asynchronous code Preserve reference to function we want call Create worker threads using libuv Handle the results
  • 29. See the example here https://github.com/NickNaso/conf-cd-rome-2018/tree/master/02-async- worker
  • 30. AsyncWorker You cannot call JavaScript function from the Worker Thread until this PR will be landed
  • 31. • ObjectWrap is a way to expose your C++ code to JavaScript • You have to extend ObjectWrap class that includes the plumbing to connect JavaScript code to a C++ object • Classes extending ObjectWrap can be instantiated from JavaScript using the new operator, and their methods can be directly invoked from JavaScript • Unfortunately, the wrap part really refers to a way to group methods and state • It’s your responsibility write custom code to bridge each of your C++ class methods. ObjectWrap API
  • 32. Much of the Node.js core API modules are built aroud an idiomatic asynchronous event-driven architecture in which certains kinds of objects (called emitter) periodically emit named events that cause Function objects ("listeners") to be called. • Emit event from C++ • Implement a native add-on object that inherits from Event Emitter Event Emitter
  • 33. See the example here https://github.com/NickNaso/conf-cd-rome-2018/tree/master/04-addon- event-emitter
  • 34. A stream is an abstract interface for working with streaming data in Node.js. The stream module provides a base API that makes it easy to build objects that implement the stream interface. Using Transform stream to pass and get back data from a native add-on Stream
  • 35. See the example here https://github.com/NickNaso/conf-cd-rome-2018/tree/master/05-addon- stream
  • 36. • Create simple interface between C / C++ and JavaScript • Think carefully at JavaScript interface • Use ObjectWrap API to return C / C++ object to JavaScript • Validate the types on the native side • Expose only the functionalities you need • Don’t block the event loop, stay asynchronous: this is a must if you will use the add-ons in high performance services • Use Buffer to pass big quantity of data Lessons learned
  • 37. • The largest number of native add-ons is written using NAN • N-API will be go out of experimental very soon • Backports for 8.x • Backports for 6.x • Documentation for node-addon-api • Porting preexistentes native addon-ons to N-API • Investigate how to use prebuild with N-API Present and future
  • 38. Implement key value database Binding to Vedis an embeddable datastore C library
  • 39. Thanks! https://github.com/NickNaso/conf-cd-rome-2018 All examples and materials are here Don’t be afraid sometimes it’s a good thing dirty your hands with a little of C++ [email protected] @NickNaso on Twitter

Editor's Notes

  • #2: Good afternoon everyone. Thank you to attend my talk, today I’m very honored to be here and share my ideas with all of you.
  • #3: My name is Nicola Del Gobbo and I’m developer at Packly. In my every day work I take care of building Packly’s backend systems. In my spare time I try to give my contribution to all frameworks, libraries and modules that I use in my work and obviously I like creating new one. Today the talk is focused on Node.js native addon start with its definition.
  • #4: In a very simple way Native Addons could be considered as C / C++ code called from JavaScript. They are a bridge between our Application programming language JavaScript and Native environment that is completely written in C / C++. This allow us to call C / C++ functions and methods directly from JavaScript.
  • #5: If we take a look at Node.js documentation under Addons section Native Addons are defined as: dynamically-linked shared objects, written in C++, that can be loaded into Node.js using the require() function, and used just as if they were an ordinary Node.js module Their main purpose is to provide an interface between JavaScript running in Node.js and C / C++ libraries. This increase the developer experience because if you are using a Native Addon you don’t care of this, but you just use it as any other pure JavaScript module.
  • #6: How is it possible? All the magic is behind the Node.js architecture. Node.js is built on some C / C++ libraries the most important for us as native addon developers are V8 and libuv. V8 is a JavaScript engine that take care to compile JavaScript code, execute it and give us great performance. Through the V8 APIs we can create function, object and do almost everything that we do on the JavaScript side. Libuv is a high performance evented I/O library and has the responsibility to handle the event loop (our main thread) and all the asynchronous tasks.
  • #7: Why should you implement new Native Add-On? The first reason is for performance In general C / C++ code performs better then JavaScript code, but it’s really true for CPU bounds operations. Think for example at: Image processing | Video processing | Compression | Scientific calculus | Cyclic redundancy check and all algorithms that at one point execute some CPU heavy tasks. In all these cases you can gain more in performance.
  • #8: Sometimes you want just integrate a legacy application So you have the source code of an old C / C++ application and want to expose something of its functionalities through new Node.js application
  • #9: Even if npm is a very large repo where you can find almost everything sometimes you don’t find what fits your specific needs and in this case you have to take a choice. Completely reimplementing the module in JavaScript, but in my opinion it’s an insane idea to reimplement from the ground solutions like ImageMagick | Ghostscript | Ffmpeg and TensorFlow because you will lose all your time on develop a module and not to concentrate on business logic of your application. In these cases the You have to concentrate on business logic of your application and not on developing a single module
  • #17: N-API is one of the most important features announced with Node.js 8 which is aimed at reducing the maintenance cost for native add-ons
  • #18: When I start to develop a new native addon I use the boilerplate depicted on the slide. Here the most important elements are the SRC and LIB folders. SRC folder contains the C / C++ code and the native dependencies while the LIB folder contains the JavaScript code There are also the usual package.json file and the binging.gyp file that contains all the building configurations for the addon. At the end our addon will be composed by two parts one written in C++ and another written in JavaScript and usually the JavaScript part will use the native code to expose some features
  • #19: When I start to develop a new native addon I use the boilerplate depicted on the slide. Here the most important elements are the SRC and LIB folders. SRC folder contains the C / C++ code and the native dependencies while the LIB folder contains the JavaScript code There are also the usual package.json file and the binging.gyp file that contains all the building configurations for the addon. At the end our addon will be composed by two parts one written in C++ and another written in JavaScript and usually the JavaScript part will use the native code to expose some features
  • #20: When I start to develop a new native addon I use the boilerplate depicted on the slide. Here the most important elements are the SRC and LIB folders. SRC folder contains the C / C++ code and the native dependencies while the LIB folder contains the JavaScript code There are also the usual package.json file and the binging.gyp file that contains all the building configurations for the addon. At the end our addon will be composed by two parts one written in C++ and another written in JavaScript and usually the JavaScript part will use the native code to expose some features
  • #21: In our binding.gyp we need to set the target name that will match the module name. The reported example is almost simple sometimes finding the right settings for binding.gyp is not easy so I suggest to take a look at GYP documentation or at nodey-gyp wiki where there are examples of existing binding.gyp and use them as source of inspiration to solve your problems with these kind of configurations.
  • #22: What happen here? The macro NODE_API_MODULE creates code that will register a module named ”echo” and in addition it will ensure that a function init is called when the module is required. In the init we export our function or object. In the reported example we export echo and you can find the code executed by this function in the method Echo. As first thing we validate the input data, then transfer the JavaScript input to C++ data structure. C++ code perform the requested computation and before returning transfer the output data to JavaScript context. This is the most straightforward integration pattern that you can realize to pass data between JavaScript and C / C++ . The main reason for using this approach is simplicity and because you can integrate the C++ without modification. In addition we have a complete decoupling of the JavaScript and C++ code.
  • #25: In an asynchronous addon function, the calling JavaScript code returns immediately. The calling code passes a callback function to the addon, and the addon does its work in a separate worker thread. This avoids locking up the Node.js event loop, as the addon function does not block.