# Integrating C++ classes

**URL:** <https://discourse.nodered.org/t/integrating-c-classes/22377>\
**Category:** General\
**Created:** [28 February 2020 13:22 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377 "2020-02-28T13:22:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![guitarpicva](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/guitarpicva/32/17069_2.png) [@guitarpicva](https://discourse.nodered.org/u/guitarpicva)\
**Post date:** [28 February 2020 13:22 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/1 "2020-02-28T13:22:32Z")

</div>

Where does one find the definitive guide on integrating back end C++ (or other language) classes in order to do complex business logic in the "background"?

Best practices? Practical advice? I work primarily in C++ (Qt).

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [28 February 2020 13:26 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/2 "2020-02-28T13:26:48Z")

</div>

Have a look at how you can create C++ Addons in Node.js - [https://nodejs.org/api/addons.html](https://nodejs.org/api/addons.html)

This allows you to create a module in C++ that can be loaded with `require` into a node.js module. So you first create your C++ module, then wrap it in node.js to create a Node-RED node in the palette - [https://nodered.org/docs/creating-nodes/](https://nodered.org/docs/creating-nodes/)

We don't have a node-red specific guide for doing the C++ part as its beyond the scope of our documentation.

---

<div class="post-metadata">

**Author:** ![guitarpicva](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/guitarpicva/32/17069_2.png) [@guitarpicva](https://discourse.nodered.org/u/guitarpicva)\
**Post date:** [28 February 2020 13:29 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/3 "2020-02-28T13:29:38Z")

</div>

Marvelous! Thank you for the direction. I have a few use cases already that I can work on as I learn. Much appreciated.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 February 2020 13:44 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/4 "2020-02-28T13:44:16Z")

</div>

Isn't nodejs inherently single threaded, so the C++ module would not actually operate in the background, or are there ways around this?

---

<div class="post-metadata">

**Author:** ![guitarpicva](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/guitarpicva/32/17069_2.png) [@guitarpicva](https://discourse.nodered.org/u/guitarpicva)\
**Post date:** [28 February 2020 14:09 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/5 "2020-02-28T14:09:55Z")

</div>

"background" is not a literal threading term here.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 February 2020 14:18 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/6 "2020-02-28T14:18:13Z")

</div>

OK, so what you want to do is to include C++ code in a node instead of using javascript.

---

<div class="post-metadata">

**Author:** ![guitarpicva](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/guitarpicva/32/17069_2.png) [@guitarpicva](https://discourse.nodered.org/u/guitarpicva)\
**Post date:** [28 February 2020 14:23 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/7 "2020-02-28T14:23:35Z")

</div>

Your question was the reason for MY query. What options exist then? The Node.js add-on makes sense, and appears to be a "callback" sort of arrangement which is sensible for asynchronous data producers/consumers.

Part of the question revolves around being able to encapsulate logic in compiled programs for various reasons. Some of those reasons involve information security and encryption key management, which would not be looked at favorably by a certification agency if implemented in JavaScript in plain text on a "web server".

So while a "key management" application might be a Node.js/Red front end, the back end that drives the key management business logic needs to be as obfuscated as it can possibly be. Additionally, any key management/logic libraries that might be provided for a specific application will likely be C++ and "untouchable".

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 February 2020 14:36 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/8 "2020-02-28T14:36:27Z")

</div>

It was more of a statement of what I understood you meant rather than a question. Doesn't @knolleary's suggestion do the job?

---

<div class="post-metadata">

**Author:** ![guitarpicva](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/guitarpicva/32/17069_2.png) [@guitarpicva](https://discourse.nodered.org/u/guitarpicva)\
**Post date:** [28 February 2020 15:09 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/9 "2020-02-28T15:09:22Z")

</div>

Yes, I believe it does. I think I have such a library that I can wrap and test with Node.js. It should prove interesting and turn some heads.

Thank you both for pitching in.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [28 February 2020 15:55 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/10 "2020-02-28T15:55:34Z")

</div>

> [@knolleary](#):
>
> This allows you to create a module in C++ that can be loaded with `require` into a node.js module.

Just idle curiosity here, but does the Addon have to be wrapped in a contributed node, or can it be loaded with `require` in `settings.js` and used in a `function` node?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [28 February 2020 15:56 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/11 "2020-02-28T15:56:58Z")

</div>

> [@drmibell](#):
>
> can it be loaded with `require` in `settings.js` and used in a `function` node

Yes it could be. The Addons are loaded using `require` and the code loading it is completely unaware of whether it was implemented in JavaScript or otherwise.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [28 February 2020 16:06 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/12 "2020-02-28T16:06:38Z")

</div>

Thanks, Nick. That opens interesting possibilities, since I have bits of legacy C code that NR talks to by TCP or MQTT. It's not worth changing those, but if I run into similar situations in the future, I might sit down and study the API.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 February 2020 16:36 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/13 "2020-02-28T16:36:03Z")

</div>

Time to go down some Node.js rabbit holes it sounds like 😀

Node is incredibly powerful and flexible.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [28 February 2020 16:49 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/14 "2020-02-28T16:49:04Z")

</div>

> [@TotallyInformation](#):
>
> Node.js rabbit holes

Yup. I still feel like I've gone through the looking-glass, where JavaScript is a Mad Hatter's tea party full of odd and inconsistent programming ideas. Learning to go with the flow (pun intended).

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 February 2020 16:56 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/15 "2020-02-28T16:56:42Z")

</div>

I would be a bit worried about the maintenance 'costs' of this route. I can't help thinking that using MQTT or similar as an interface might be much simpler and you wouldn't be worried about changes with nodejs version and so on. Perhaps it wouldn't be that bad though, more learning would be required to fully understand the implications.  
An advantage of MQTT is that you get multi-threading, but there would obviously be run time overheads, so it depends on the requirement.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [28 February 2020 18:37 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/16 "2020-02-28T18:37:53Z")

</div>

> [@Colin](#):
>
> worried about changes with nodejs version and so on

Apparently, one of the options for implementing Addons (N-API) is intended to survive changes in the JS engine and not require re-compilation for new versions of node.js. That's all I "know" on the subject. I'll be watching to see what @guitarpicva can produce.

---

<div class="post-metadata">

**Author:** ![guitarpicva](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/guitarpicva/32/17069_2.png) [@guitarpicva](https://discourse.nodered.org/u/guitarpicva)\
**Post date:** [28 February 2020 20:02 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/17 "2020-02-28T20:02:47Z")

</div>

From a support perspective, my deployed systems would rarely change their versions of any underlying tools.

Part of why I am going down this thought road is to assess the viability of Node Red as a customer deployed system which includes hardware and software. The people in the particular market I serve generally are not systems people to any great degree. To them this would be a black box that does magic.

> [@drmibell](#):
>
> I still feel like I've gone through the looking-glass, where JavaScript is a Mad Hatter's tea party full of odd and inconsistent programming ideas.

It appears that my resistance to JavaScript menagerie has come to an abrupt end as well.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 February 2020 20:35 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/18 "2020-02-28T20:35:56Z")

</div>

Do remember though that server-side JavaScript is still a fast-moving arena so you will find yourself needing to update things perhaps more often that you had previously. This is especially true if your systems cross network boundaries and particularly if they are accessible from the Internet.

---

<div class="post-metadata">

**Author:** ![guitarpicva](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/guitarpicva/32/17069_2.png) [@guitarpicva](https://discourse.nodered.org/u/guitarpicva)\
**Post date:** [28 February 2020 20:57 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/19 "2020-02-28T20:57:01Z")

</div>

> [@TotallyInformation](#):
>
> you will find yourself needing to update things perhaps more often that you had previously

Are client browser changes the target of that warning or only the server side??

I should also clarify that "key management" has nothing to do with web api's or authentication. This is straight up encryption of data being transmitted by other means outside the internet. None of the systems I refer to would necessarily even be internet connected.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 February 2020 22:12 UTC](https://discourse.nodered.org/t/integrating-c-classes/22377/20 "2020-02-28T22:12:34Z")

</div>

> [@guitarpicva](#):
>
> Are client browser changes the target of that warning or only the server side??

The server. Node.js is still updated very regularly with important fixes including security fixes. Similarly node.js modules (including Node-RED modules) are updated very rapidly. Even more so now that people are regularly getting security warnings about their modules from GitHub and other sources. Also partly due to the large number of modules the average module includes so that the dependency map is very deep.

While it is great that things are being kept current and regularly updated, it can cause a bit of a headache for an administrator.

Not a big problem generally, just something to be aware of if you are used to slow-moving developments.

[Next page](https://discourse.nodered.org/t/integrating-c-classes/22377.md?page=2)
