# Node-red C program communication

**URL:** <https://discourse.nodered.org/t/node-red-c-program-communication/59225>\
**Category:** General\
**Created:** [2 March 2022 10:49 UTC](https://discourse.nodered.org/t/node-red-c-program-communication/59225 "2022-03-02T10:49:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oliver98t](https://avatars.discourse-cdn.com/v4/letter/o/a8b319/32.png) [@Oliver98t](https://discourse.nodered.org/u/Oliver98t)\
**Post date:** [2 March 2022 10:49 UTC](https://discourse.nodered.org/t/node-red-c-program-communication/59225/1 "2022-03-02T10:49:58Z")

</div>

I am attempting to pass in and extract data from a C program from my node-red application. I have successfully been able to pass out data in my C program from stdout into node red but am unsure as to how to get data into my C program from node-red. Will I need to use some sort of inter process mechanism like linux d-bus etc?

Any suggestions would be greatly appreciated.

---

<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:** [2 March 2022 11:07 UTC](https://discourse.nodered.org/t/node-red-c-program-communication/59225/2 "2022-03-02T11:07:29Z")

</div>

Assuming your application is long running (i.e. NOT a fire up, process args, write to STDOUT, quit), a common approach is to have your application communicate to node-red via MQTT.

There are a few C libs for MQTT.

If you are not familiar with MQTT, I can assure you it is far easier that setting up pipes or busses etc. (just ask)

---

<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:** [2 March 2022 11:08 UTC](https://discourse.nodered.org/t/node-red-c-program-communication/59225/3 "2022-03-02T11:08:48Z")

</div>

Can you do it just using command line parameters to the program?

---

<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:** [2 March 2022 11:36 UTC](https://discourse.nodered.org/t/node-red-c-program-communication/59225/4 "2022-03-02T11:36:48Z")

</div>

> [@Oliver98t](#):
>
> how to get data into my C program from node-red. Will I need to use some sort of inter process mechanism like linux d-bus etc?

Depends how complex the data is. For simple data, Colin's suggestion of command line parameters may be sufficient. For text-based but longer data, you could make your C++ app accept piped input from std-in.

For more complex data or better performance most of what you may choose to use would need you to turn your app into a daemon/service so that it could be listening on a port or pipe. Probably not what you want. You could also write your c++ code as a library and then wrap it with a Node.js module that could be used inside Node-RED.

You may wish to avoid platform-specific IPC though since that restricts what platforms you can run on - which may or may not be an issue for you.

---

<div class="post-metadata">

**Author:** ![Oliver98t](https://avatars.discourse-cdn.com/v4/letter/o/a8b319/32.png) [@Oliver98t](https://discourse.nodered.org/u/Oliver98t)\
**Post date:** [2 March 2022 11:44 UTC](https://discourse.nodered.org/t/node-red-c-program-communication/59225/5 "2022-03-02T11:44:00Z")

</div>

The C application will be long running and sending floats, nothing too fancy. I would also like to send data (floats, ints etc) to the C program whilst its running so I don't think I would be able to pass that data into the C program using command line parameters. Hope that paints a better picture of what I'm trying to do.

---

<div class="post-metadata">

**Author:** ![Oliver98t](https://avatars.discourse-cdn.com/v4/letter/o/a8b319/32.png) [@Oliver98t](https://discourse.nodered.org/u/Oliver98t)\
**Post date:** [2 March 2022 11:47 UTC](https://discourse.nodered.org/t/node-red-c-program-communication/59225/6 "2022-03-02T11:47:13Z")

</div>

Thanks for the suggestion Steve!

---

<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:** [2 March 2022 11:51 UTC](https://discourse.nodered.org/t/node-red-c-program-communication/59225/7 "2022-03-02T11:51:45Z")

</div>

> [@Oliver98t](#):
>
> The C application will be long running and sending floats, nothing too fancy. I would also like to send data (floats, ints etc) to the C program whilst its running so I don't think I would be able to pass that data into the C program using command line parameters. Hope that paints a better picture of what I'm trying to do.

In that case, Node-RED can use many different IPC methods - though I've never actually gotten round to trying UNIX pipes 🙂

But if you are happy to have another service running (the MQTT broker), certainly MQTT makes this pretty straight-forwards though at the cost of some bloat in your C++ app since you will need a suitable MQTT library linked or compiled in. But truthfully, if your C++ programme can create a TCP or UDP listener and sender, that might be enough. But MQTT will give you more flexibility of course and some pre-built structure to work with.

---

<div class="post-metadata">

**Author:** ![Oliver98t](https://avatars.discourse-cdn.com/v4/letter/o/a8b319/32.png) [@Oliver98t](https://discourse.nodered.org/u/Oliver98t)\
**Post date:** [2 March 2022 11:58 UTC](https://discourse.nodered.org/t/node-red-c-program-communication/59225/8 "2022-03-02T11:58:14Z")

</div>

I'll experiment with both the MQTT approach first as this seems more straight forward than using UNIX pipes. I will however investigate using pipes as this seems to be more efficient, are there any resources you know that would help me get started with UNIX pipes?

---

<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:** [2 March 2022 12:05 UTC](https://discourse.nodered.org/t/node-red-c-program-communication/59225/9 "2022-03-02T12:05:07Z")

</div>

> [@Oliver98t](#):
>
> are there any resources you know that would help me get started with UNIX pipes?

Well they seem to be one of UNIX's (actually I think they may be a POSIX standard) better kept secrets. So I've never found a lot of information that didn't need a lot of head-scratching and re-reading 🌩

But I have played with them and got them working between Node.js apps in the past - my understanding is that Node-RED should also allow them in some places but I'm afraid that I've not tried.

This might help though: [node-red-contrib-ipc (node) - Node-RED (nodered.org)](https://flows.nodered.org/node/node-red-contrib-ipc)

---

<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:** [2 March 2022 17:47 UTC](https://discourse.nodered.org/t/node-red-c-program-communication/59225/10 "2022-03-02T17:47:24Z")

</div>

You can use the node-red-node-daemon node to run your program. Then the msg.payload is fed to STDIN. And receives stout and std err like the exec node does

---

<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:** [1 May 2022 17:48 UTC](https://discourse.nodered.org/t/node-red-c-program-communication/59225/11 "2022-05-01T17:48:05Z")

</div>

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