How to connect NodeJS and Java?

Viewed 84

I need for my Node application to be able to send and recieve data to/from an already running Java app or to launch Java app from Node.

So, is there a way to send data from NodeJS to Java and back?

My goal is to have something like this:

// In Java:
J2JSInterface.setReciever(msg -> System.out.println("Message from JS: " + msg));
J2JSInterface.send("Hello from Java!");

// In Java console:
// Message from JS: Hello from JS!
// In Node:
JS2JInterface.setReciever(msg => console.log(`Message from Java: ${msg}`))
JS2JInterface.send("Hello from JS!")

// In Node console:
// Message from Java: Hello from Java!

P.S.: I Tried using JNI and Node Api in one C++ library together, (code) but it won't work because Node and Java will load two independent instances of the library and these instances can't interact with each other. (or can they?)

1 Answers

I really tend to overcomplicate things...

TCP is the solution for really fast communicating. When I checked the latency, I was shocked, because it was around 0 to 1 ms. Thanks to AhmetUğur for pointing out that I could just use websockets!

Related