Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inter-process communication between node and C application

Tags:

c

node.js

ipc

I have 2 software componnects I'd like to make talk to each other,

  1. A node.js web application
  2. A dedicated server written in C (a fairly simple piece of code that deals with some obscure library that i'd rather not wrap for other languages)

The conversations I'd like to have are pretty simple,

  • Node: Setup resource id A
  • C app: Ok, here's the ref num

or

  • Node: Take down this ref
  • C app: Sure, mate.

Is there any painless way to pass messages between the 2?

My theoretical idea currently is something along these lines:

  1. Have 1 tcp/unix socket between the 2 processes, 1 session only to avoid opening and closing sessions all the time (also afraid there'd be too much at once).
  2. Each matching node request and C app response bear same ID (tcp-esque sessions over single tcp session)
  3. Each relevant node request would generate message to C app, have the response objects stored in some hash with session ids as keys.
  4. Have a single node thread to collect C app replies, find response objects by session id and respond to client

Is it terribly inefficient?

Is there actually thread support in node? (a short google didn't bring up any concrete results)

like image 411
DimaK Avatar asked Oct 04 '22 00:10

DimaK


1 Answers

Basically you are in the right direction, you need some kind of inter-process-communication. It really depends how complex your IPC is. If it is just about some calling of methods and sharing simple (i.e. integer, strings) data between methods, you could live with unix sockets, that are fairly easy to implement and are supported by nodejs natively and are pretty easy to use from C as well (because the needed headers are almost always already available)

ZeroMQ gives you transport abstraction (allowing for IPC or true network-based transports) and a lot of bindings for even obscure languages. The provided examples for C and Node should get you going in no time. However you need the corresponding headers and libraries; that most likely need to be build first.

If the data that you are going to exchange between your programs is going to be more complex (read structs or arrays, nested objects), then you will need some real RPC implementation. There are a bunch of RPC implementations out there; feature-wise Apache Thrift looked very promising to me, although in the end I could not get the thrift compiler being build in a reasonable time.

So in the end, for me, having a rather similar use case, I ended up with ZeroMQ for the transport abstraction and on top of that using JSON-RPC as RPC mechanism. JSON-RPC is pretty native for nodejs and for C I used https://github.com/pijyoi/jsonrpc , that builds on ZeroMQ and Jansson. The documentation is non-existent but if you come that far, you shouldn't be afraid.

Performance-wise I don't think that this is going to be a problem; your use case does not sound, like you have a lot of requests in a short time. As ZeroMQ also offers traditional IPC as a transport, you could also use that to improve performance.

like image 98
Christian Ulbrich Avatar answered Oct 13 '22 10:10

Christian Ulbrich