Node-red C program communication

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.

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)

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

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.

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.

Thanks for the suggestion Steve!

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

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.

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?

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 :cloud_with_lightning:

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)

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

1 Like

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