# Help with creating a new node with c++

**URL:** <https://discourse.nodered.org/t/help-with-creating-a-new-node-with-c/41713>\
**Category:** Developing Nodes\
**Created:** [27 February 2021 13:01 UTC](https://discourse.nodered.org/t/help-with-creating-a-new-node-with-c/41713 "2021-02-27T13:01:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![theo481](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@theo481](https://discourse.nodered.org/u/theo481)\
**Post date:** [27 February 2021 13:01 UTC](https://discourse.nodered.org/t/help-with-creating-a-new-node-with-c/41713/1 "2021-02-27T13:01:41Z")

</div>

Hi everyone,  
Still being quite new ( with node-red, node.js and javascript in general) so understanding code online is not easy; I was therefore hoping to find some help over her.

I wrote some code in c++, that I managed to have it called through node.js

All good so far, but I am not really sure how to proceed next..

I do have my binding.gyp file ( where all my functions and header files are called), main.js ( and all the ones created when building my program ), ending up with a main.js file that looks like this :

const calculate = require('./build/Release/sensor')  
console.time('c++')  
console.log(sensor.calc());  
console.timeEnd('c++');

with my C++ program returning a floating number( I d like to update it to return an array since my sensor returns both temperature and humidity );

My question now is, how could I take these 4 lines of code and embed them in node-red?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 February 2021 13:46 UTC](https://discourse.nodered.org/t/help-with-creating-a-new-node-with-c/41713/2 "2021-02-27T13:46:43Z")

</div>

Is it that you are more familiar with C++ so avoiding learning how to do this stuff natively in node-red?

TBH, unless you are doing some heavy computation or something not possible in nodejs then I would suggest you are missing out on the flexibility / dynamic programmable runtime aspect of node-red.

> [@theo481](#):
>
> My question now is, how could I take these 4 lines of code and embed them in node-red?

Well, there is a way of requiring a a non node-red js module ([see here](https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules)) then you can use a function node to do the `require` and call your code.

However, there are other options. for example...

- if you built MQTT into your C++ application, you can simply use MQTT for the transfer of data. And if you send your data out in JSON format, it is a really simple task of accessing your property values without resorting to string splitting etc.
- (or) you could write a custom contrib node-red node so all users could benefit from your works & install it from the palette

> [@theo481](#):
>
> with my C++ program returning a floating number( I d like to update it to return an array since my sensor returns both temperature and humidity );

As this is a JS environment, you would be far better off returning a JSON string & using the JSON node to convert that back into an object with nice property names were you could simple access `msg.payload.temperature` and `msg.payload.humidity`

* * *

### EDIT:

My apologies, I see you are actually trying to create a node. (should read full message before posting).

So to answer your question "with my C++ program returning a floating number( I d like to update it to return an array since my sensor returns both temperature and humidity );" - JSON is the way to go. Then in your custom node, simple do something like `msg.payload = JSON.parse(sensor.calc())` to convert the JSON sent from your C++ to a native object.

### EDIT2:

is there an absolute need to use C++ - is it not possible to use nodejs under the hood?

---

<div class="post-metadata">

**Author:** ![theo481](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@theo481](https://discourse.nodered.org/u/theo481)\
**Post date:** [27 February 2021 14:00 UTC](https://discourse.nodered.org/t/help-with-creating-a-new-node-with-c/41713/3 "2021-02-27T14:00:13Z")

</div>

hi steve, thnx for the reply,  
Yes, C++ is by far the language I'm the most familiar with, especially for the I2C readings in Linux part ( want to run my code on a BBB ), and although I'm trying to learn how to do it natively, I d feel bad having to start all over again, and will therefore try to do that as a next step once I have everything working 🙂

Once everything is working, I'll gladly share my nodes, together with my findings because I don't think(at least I hope) that I 'm the only one struggling with it.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 February 2021 14:09 UTC](https://discourse.nodered.org/t/help-with-creating-a-new-node-with-c/41713/4 "2021-02-27T14:09:36Z")

</div>

Perhaps, for starters, you could try out your bindings using the function node + [loading-additional-modules](https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules) that i linked to earlier

Once you have things working, you can develop a custom node (see [here](https://nodered.org/docs/developing-flows/) for info)

One thing I find helps with development of contrib nodes is to take a look at existing nodes & view its source code (lots of contrib nodes in github)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 April 2021 14:09 UTC](https://discourse.nodered.org/t/help-with-creating-a-new-node-with-c/41713/5 "2021-04-28T14:09:37Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
