Home » Nodejs » Nodejs communicate with C++ program?

Nodejs communicate with C++ program?

Posted by: admin November 30, 2017 Leave a comment

Questions:

Assume I’ve got a c++ program running on the same server with the web server by Node.js.

The Cpp program maintains a queue, what I want to do with Node.js is, pull some data into the queue.

What’s the best way to do this?

Which is better? sockets or IPC?

(os is linux)

Answers:

If you’re using Linux, I would suggest UNIX-domain sockets. They basically give you the high-performance of IPC using the BSD socket interface, making it easy to switch for TCP sockets later if you need to move the C++ (or node.js) application to a different computer.

They’re already supported by node.js and only the code that opens the socket will need to be changed. Many applications, including MySQL easily abstract this away in a configuration file.

Questions:
Answers:

I’d use sockets, they are clean and easy to use

Questions:
Answers:

If you want to use an IPC mechanism, you may consider writing a Node.js C++ module, and then use something from the Boost.Interprocess library to communicate with your other app.

Boost.Interprocess has mechanisms already build for sharing containers from the standard library. Its also cross platform if you want to be open to that in the future.