Skip to content

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

Closed
@ibc

Description

@ibc

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions