IPC Communication between Node.js and Java

Issue

I’m looking to communicate between Node.js and a Java sub-process via IPC. I’m looking for recommendations that do not require opening a port.

const { spawn } = require('child_process')

// Java App is within args
const args = []

const javaProcess = spawn('java', args, { cwd: __dirname })

javaProcess.on('message', (m) => {
  console.log('PARENT got message:', m)
})

// Sends { 'hello': 'world' } to the Java process
javaProcess.send({ hello: 'world' })

I understand that the Node layer can send messages to a sub-process, I just don’t know how I’d go about listening for messages in the Java layer and sending messages back.

Any help is greatly appreciated, thanks!

Solution

I solved this issue by using named pipes…

On Node I used the Net API
https://nodejs.org/api/net.html#net_ipc_support

On Java I used RandomAccessFile
https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html

Answered By – Mike Mulligan

Answer Checked By – Dawn Plyler (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.