16

Say I've got a c++ program running on the same server with a Node.js web app, on a linux server.

The c++ program maintains a queue, and what I want to do with Node.js is, push some data into the queue.

What's the best way to do this?

Which is better? sockets or IPC?

NamiW
  • 1,572
  • 4
  • 19
  • 33
  • We used `dbus` to communicate between C++ and nodejs on our embedded system. DBUS is a default bus on linux systems. – Frank Roth May 09 '17 at 11:31

3 Answers3

13

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.

Community
  • 1
  • 1
André Caron
  • 44,541
  • 12
  • 67
  • 125
2

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

luke14free
  • 2,529
  • 1
  • 17
  • 25
  • but actually they're on the same server, I'm wondering socket may be slow than IPC things, am I right? – NamiW Mar 23 '12 at 16:59
  • it is definitely slower but it will prepare your app to scale if someday the two wont reside anymore on the same server. – luke14free Mar 23 '12 at 17:17
  • @luke14free: that depends. Unix sockets might be faster than regular TCP sockets. – André Caron Mar 23 '12 at 17:51
  • @AndréCaron ICP = Interprocess Communications Protocol not TCP ;) – luke14free Mar 23 '12 at 18:04
  • 2
    @luke14free: Google has not heard of an ICP "interprocess communications protocol". I think you meant [IPC = Inter-Process Communication](http://en.wikipedia.org/wiki/Inter-process_communication). Since OP specified Linux is the OS, the best platform-specific IPC likely to be supported by node.js is [Unix-domain sockets](http://en.wikipedia.org/wiki/Unix_domain_socket), which are likely to give better performance than TCP sockets. – André Caron Mar 23 '12 at 18:56
  • this is totally false. a signal to a process is way faster than a socket communication – luke14free Mar 23 '12 at 20:15
  • @luke14free: I'm not sure exactly what you claim to be false. Besides, signals are mostly faster than other IPC methods because they don't carry any custom data payloads. Even though [node.js supports UNIX signals](http://dailyjs.com/2012/03/15/unix-node-signals/), signals and sockets are not interchangeable methods for IPC. – André Caron Mar 24 '12 at 06:33
  • take a look here anyhow http://stackoverflow.com/questions/1235958/ipc-performance-named-pipe-vs-socket – luke14free Mar 24 '12 at 10:12
2

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.

Zac
  • 3,235
  • 4
  • 25
  • 30