Blank Chisui
Blank Chisui

Reputation: 1073

continuously read json from childprocess stdout

I want to read and send json messages in nodejs from and to a java childprocess. The childprocess will live for some time. Messages sent and recieved via stdio represent events from and to the two processes. As a result the messages have to be processed as soon as they are completed.

The Output will be in the form

{"type":"eventType","data":...}
{"type":"anotherEventType","data":...}
...
{"type":"anotherEventType","data":...}

Writing the messages is no problem but reading is. Especially since I can not rely on the messages splitting on \n nor that one chunk contains exactly one message.

Unfortunately every example I could find buffers the output of stdout until the process terminates and only than parses it. And all IPC node modules I could find use sockets which I would like to avoid.

Are there existing libraries that provide such functionality or do I have to role my own?

Upvotes: 1

Views: 1565

Answers (1)

Blank Chisui
Blank Chisui

Reputation: 1073

JSONStream does exactly what I want.

child.stdout
    .pipe(require('JSONStream').parse())
    .on('data', processMessage);

Upvotes: 2

Related Questions