Interprocess communication between node and C application

I have 2 software components that I would like to talk to each other,

  • Node.js web application
  • a dedicated server written in C (a fairly simple piece of code that deals with some kind of obscure library that I would rather not transfer to other languages)

The connections I would like to have are pretty simple,

  • Node: specify resource identifier A
  • C: Ok here ref num

or

  • Node: Remove this ref
  • Appendix C: Of course, buddy.

Is there a painless way to pass messages between 2?

My theoretical idea currently has something like that:

  • You have 1 tcp / unix socket between 2 processes, 1 session to open and close sessions all the time (you were also afraid that there would be too much at once).
  • Each corresponding node request and C application response have the same identifier (tcp-esque sessions in one tcp session)
  • Each corresponding node request generates a message in application C, has response objects stored in some hashes with session identifiers as keys.
  • Have a single node thread to collect responses to C applications, search for response objects by session ID and response to client

Is it really terribly inefficient?

Is there thread support in node? (short google did not bring any specific results)

+4
source share
1 answer

Basically, you are in the right direction, you need some kind of interprocess communication. It really depends on how complex your IPC is. If we are only talking about some method calls and sharing simple (i.e., Integer, string) data between methods, you can live with unix sockets that are pretty easy to implement and are supported by nodejs initially and pretty easy to use from C like ( because the right headers are almost always available)

ZeroMQ provides you with a transport abstraction (allowing IPC or real network transports) and many bindings for even obscure languages. The above examples for C and Node should help you as soon as possible. However, you need appropriate headers and libraries; which, most likely, will need to be created first.

If the data you are going to exchange between your programs will be more complex (read structures or arrays, nested objects), then you will need a real RPC implementation. There are many RPC implementations; The special Apache Thrift looked very promising for me, although in the end I couldn't get the thrift collector to build in a reasonable amount of time.

So, in the end, for me, having a pretty similar use case, I ended up in ZeroMQ for transport abstraction and, moreover, using JSON-RPC as an RPC mechanism. JSON-RPC is pretty popular for nodejs, and for C I used https://github.com/pijyoi/jsonrpc , which is based on ZeroMQ and Jansson. Documentation does not exist, but if you go this far, you should not be afraid.

In terms of performance, I do not think this will be a problem; your use case doesn't sound like you have a lot of queries in a short time. Since ZeroMQ also offers traditional IPC as a transport, you can also use it to improve performance.

+4
source

Source: https://habr.com/ru/post/1483028/


All Articles