Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

child_process.spawn() with 'ipc': has JSON built-in? #48

Closed
ibc opened this issue Dec 12, 2015 · 4 comments
Closed

child_process.spawn() with 'ipc': has JSON built-in? #48

ibc opened this issue Dec 12, 2015 · 4 comments

Comments

@ibc
Copy link

ibc commented Dec 12, 2015

Hi, the doc says:

'ipc' - Create an IPC channel for passing messages/file descriptors between parent and child. A ChildProcess may have at most one IPC stdio file descriptor. Setting this option enables the ChildProcess.send() method. If the child writes JSON messages to this file descriptor, then this will trigger ChildProcess.on('message'). If the child is an Node.js program, then the presence of an IPC channel will enable process.send() and process.on('message').

In my case I call spawn() with stdio: ['pipe', 'pipe', 'pipe', 'pipe']to launch a C+libuv subprocess in which I open the fd 3 to intercommunicate with the Node process and then pass JSON bodies in both directions.

According to the doc above, in Node I can do process.send(JSON_BODY) and process.on('message', function(JSON_BODY), but:

  • Does it mean that process.on('message', function(JSON_BODY) directly provides a JS object? or just a string I need to parse with JSON.parse()? or a Buffer?
  • Node's IPC are stream-based pipes, so being a stream I should be ready to receive "partial" JSON bodies... or does Node some magic and it guarantees me that any string given to process.send() is totally received in the child process and also that the data/string received in process.on('message') contains all the data written by the subprocess?
@ibc
Copy link
Author

ibc commented Dec 12, 2015

By checking the code it seems that ' ipc' assumes JSON based messages with no line break (just one at the end that signals end of message).

This is clearly designed for spawning a Node subprocess. What about if my subprocess is not a Node program but a C program? What should I expect in the C side? Which kind of messages should I send from C side? Shouldn't this be documented?

@bnoordhuis
Copy link
Member

It's really only intended for communication between node processes but if your C program speaks newline-delimited JSON, there's no reason it wouldn't work.

The 'ipc' part is tricky on Windows; the C program needs to know about the ad hoc protocol for sending over handles (unless you let libuv handle that, of course.)

@ibc
Copy link
Author

ibc commented Dec 12, 2015

Thanks a lot. Anyhow I'll feel more comfortable with a simple pipe :)

@ibc ibc closed this as completed Dec 12, 2015
@ORESoftware
Copy link

@ibc did you find a good solution to this? I think IPC can be implemented with pipes. I just don't know how Node.js implements IPC, I think there is more than one way to do IPC of course, just not sure how Node.js does it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants