Reputation: 810
Let me explain my situation:
I have a (c++) module that gathers data from an application 1 at a rate of 10 times per second.
I have another (nodejs) application 2, that needs to get the data from that module.
My question is: What is the best cross-platform way to share data from the module so that the application 2 can read it?
My technique would be to write in a UDP socket from the module and to read this socket from the application 2. But since I have never done that I'm not sure that it is the best way to do it.
Upvotes: 0
Views: 299
Reputation: 5039
This depends:
Does the C++ module run on the same machine as the nodejs application? Then the best way in my opinion would be to compile and load the C++ module as a native nodejs module. So your C++ application would become a shared library i.e. DLL for nodejs, which can be called directly from the nodejs i.e. javascript application, which could bet ordinary javascript objects via a promise for example. Then you should use the NAPI from nodejs, a nice to build productive template I use can be found here.
This should be much faster and safer to use than other classical IPC (Inter process commonucation) techniques, as shared memory or message piping.
However if your applications are running on different machines, including different machines on the same physical device like VMs, you need some sort of network way. The only difference between the different machines/different physical computers and different machines/same physical computer, would be that in the later case you would use the so called loop back address, instead of a specific variable address. But from here it gets very broad of which call style, object serialization or transport protocol fits your case best, and is heavily opinion based.
A few to mention are
Since you are already in a nodejs environment, where HTTP/JSON is common, I would either try to package your C++ module in a nodejs native module even tough it has to communicate with the other nodejs instance on the other machine. But HTTP/JSON is much more easier with nodejs, and you just forward the javascript objects from your C++ module.
Or you use boost beast or asio, for a native HTTP approach, but that would consume much time.
Upvotes: 1