# Calling C code from within node-red

**URL:** https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742
**Category:** General
**Created:** [4 April 2022 01:58 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742 "2022-04-04T01:58:55Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Hishamjz](https://avatars.discourse-cdn.com/v4/letter/h/c0e974/32.png) [@Hishamjz](https://discourse.nodered.org/u/Hishamjz)
#### Post date: [4 April 2022 01:58 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/1 "2022-04-04T01:58:55Z")

</div>

Hello everybody,

I'm new to javascript. I am interested in running C code inside of node-red. I have read it is possible to run C code in Javascript code using C++ addons, is this possible to do in Node-RED? if not, is there a way to do so?  
Thanks for the help

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [4 April 2022 05:00 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/2 "2022-04-04T05:00:15Z")

</div>

C is not C++, however C & C++ code both need to be compiled to be able to run.  
Can you elaborate why you would want this ? Is it just because you are new to javascript ?

---

<div class="post-metadata">

### Author: ![Hishamjz](https://avatars.discourse-cdn.com/v4/letter/h/c0e974/32.png) [@Hishamjz](https://discourse.nodered.org/u/Hishamjz)
#### Post date: [4 April 2022 18:22 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/3 "2022-04-04T18:22:51Z")

</div>

I am using C code of an SDK to a device I am using which is Analog Discovery 2 to read measurements from it and then transferring them into Node-RED and do some signal analysis and signal depicting using charts. I am using to transfer the samples Mosquitto MQTT service but I am interested in running the C code directly in Node-RED function to be able to acess the samples without the need to use an MQTT service and thereby reducing the computational load on my PC. I hope I am able to do this inside of Node-RED.

---

<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: [4 April 2022 19:11 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/4 "2022-04-04T19:11:07Z")

</div>

It _is_ possible to run C/C++ by wrapping it in a node.js module. But that is well outside the scope of this forum. However, you can look up how to do it. Not simple though probably.

As for MQTT, assuming that you use Mosquitto as your MQTT broker, its performance overhead on your PC will be minimal. Generally a lot less than running a Microsoft Office app such as Outlook for example 😀

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [4 April 2022 20:52 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/5 "2022-04-04T20:52:51Z")

</div>

There is also Aedes, an MQTT broker that runs within Node-red. It's limited compared to mosquitto but it works, and you can be sure it's running when Node-red is.

---

<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: [4 April 2022 21:01 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/6 "2022-04-04T21:01:31Z")

</div>

It also uses a LOT more resources than Mosquitto.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [4 April 2022 21:19 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/7 "2022-04-04T21:19:44Z")

</div>

Wow, didn't know that. Good thing I switched to Mosquitto! 🦟

---

<div class="post-metadata">

### Author: ![davidz](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@davidz](https://discourse.nodered.org/u/davidz)
#### Post date: [4 April 2022 22:11 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/8 "2022-04-04T22:11:10Z")

</div>

Alternatively, you may look into "exec" node. The "exec" node is pretty efficient.  
You can call an C executable with stdin parameters.  
The node returns with C stdout parameters.

---

<div class="post-metadata">

### Author: ![Hishamjz](https://avatars.discourse-cdn.com/v4/letter/h/c0e974/32.png) [@Hishamjz](https://discourse.nodered.org/u/Hishamjz)
#### Post date: [6 April 2022 15:44 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/9 "2022-04-06T15:44:45Z")

</div>

Thank you for this tip, this could save a lot of time.

I tried it, it worked for the first couple of times but then I get code "command failed, code: 3221226356"  
I am able to run other programs, but the one I'm interested in I get this error, anyone knows what does this mean?

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [6 April 2022 16:07 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/10 "2022-04-06T16:07:21Z")

</div>

A debug node on the second output from the exec node (stderr) might give a clue what's going wrong.

---

<div class="post-metadata">

### Author: ![Hishamjz](https://avatars.discourse-cdn.com/v4/letter/h/c0e974/32.png) [@Hishamjz](https://discourse.nodered.org/u/Hishamjz)
#### Post date: [6 April 2022 16:20 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/11 "2022-04-06T16:20:35Z")

</div>

I have the node on the second output, but I don't get any output on it....means I can't see what's the issue, only the reason node is saying code number

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [6 April 2022 16:23 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/12 "2022-04-06T16:23:05Z")

</div>

Did you set the debug node to display the 'complete msg object' ?

---

<div class="post-metadata">

### Author: ![Hishamjz](https://avatars.discourse-cdn.com/v4/letter/h/c0e974/32.png) [@Hishamjz](https://discourse.nodered.org/u/Hishamjz)
#### Post date: [6 April 2022 17:44 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/13 "2022-04-06T17:44:56Z")

</div>

It worked out, I didn't know that the exec node waits until the program is completely finished to show the message payload on the debug node, I had a long for loop which was taking so much time that I thought it is not working, now I reduced the number of cycles and it worked fine, thank you guys.

One question regarding this matter, now I get 1 complete message which has a lot of samples (corresponding to the number of times the program has looped) , now I am interested in showing the results on a chart, but I have only one big message with all these individual samples, is there a way to plot this on a chart or maybe convert this big string arrab into individual values to be able plot it on a chart? thanks as always!

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [6 April 2022 17:57 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/14 "2022-04-06T17:57:19Z")

</div>

You may want to try the node in spawn mode - that may send an output per line/loop (but may depend on if your app does flush the print buffer every time or not - (If you also have source for that you can force it to flush on each loop)..

Failing that it depends what the string looks like - if just values then the split node set with whatever character separates the values may work. But if you post a sample I'm sure someone can help.

---

<div class="post-metadata">

### Author: ![davidz](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@davidz](https://discourse.nodered.org/u/davidz)
#### Post date: [7 April 2022 03:12 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/15 "2022-04-07T03:12:32Z")

</div>

@Hishamjz That's great. You may parse the big message for chart display per @dceejay . This is the beauty of the dashboard that can show charts quickly. There are many posts about how to parse a big message in this forum.

---

<div class="post-metadata">

### Author: ![aallen](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@aallen](https://discourse.nodered.org/u/aallen)
#### Post date: [7 April 2022 18:02 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/16 "2022-04-07T18:02:43Z")

</div>

You can use WaveForms program

---

<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: [21 April 2022 18:03 UTC](https://discourse.nodered.org/t/calling-c-code-from-within-node-red/60742/17 "2022-04-21T18:03:11Z")

</div>

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