@@ -52,6 +52,14 @@ in the child. After disconnecting it is no longer possible to send messages.
5252An alternative way to check if you can send messages is to see if the
5353` child.connected ` property is ` true ` .
5454
55+ ### Event: 'message'
56+
57+ * ` message ` {Object} a parsed JSON object or primitive value
58+ * ` sendHandle ` {Handle object} a handle object
59+
60+ Messages send by ` .send(message, [sendHandle]) ` are obtained using the
61+ ` message ` event.
62+
5563### child.stdin
5664
5765* {Stream object}
@@ -116,15 +124,56 @@ process may not actually kill it. `kill` really just sends a signal to a proces
116124
117125See ` kill(2) `
118126
119-
120127### child.send(message, [ sendHandle] )
121128
122129* ` message ` {Object}
123130* ` sendHandle ` {Handle object}
124131
125- Send a message (and, optionally, a handle object) to a child process.
132+ When useing ` child_process.fork() ` an you can write to the child using
133+ ` child.send(message, [sendHandle]) ` and messages are received by
134+ a ` 'message' ` event on the child.
135+
136+ For example:
137+
138+ var cp = require('child_process');
139+
140+ var n = cp.fork(__dirname + '/sub.js');
141+
142+ n.on('message', function(m) {
143+ console.log('PARENT got message:', m);
144+ });
145+
146+ n.send({ hello: 'world' });
147+
148+ And then the child script, ` 'sub.js' ` might look like this:
149+
150+ process.on('message', function(m) {
151+ console.log('CHILD got message:', m);
152+ });
153+
154+ process.send({ foo: 'bar' });
126155
127- See ` child_process.fork() ` for details.
156+ In the child the ` process ` object will have a ` send() ` method, and ` process `
157+ will emit objects each time it receives a message on its channel.
158+
159+ There is a special case when sending a ` {cmd: 'NODE_foo'} ` message. All messages
160+ containing a ` NODE_ ` prefix in its ` cmd ` property will not be emitted in
161+ the ` message ` event, since they are internal messages used by node core.
162+ Messages containing the prefix are emitted in the ` internalMessage ` event, you
163+ should by all means avoid using this feature, it is subject to change without notice.
164+
165+ The ` sendHandle ` option to ` child.send() ` is for sending a handle object to
166+ another process. The child will receive the object as its second argument to
167+ the ` message ` event.
168+
169+ ### child.disconnect()
170+
171+ To close the IPC connection between parent and child use the
172+ ` child.disconnect() ` method. This allows the child to exit gracefully since
173+ there is no IPC channel keeping it alive. When calling this method the
174+ ` disconnect ` event will be emitted in both parent and child, and the
175+ ` connected ` flag will be set to ` false ` . Please note that you can also call
176+ ` process.disconnect() ` in the child process.
128177
129178## child_process.spawn(command, [ args] , [ options] )
130179
@@ -333,38 +382,8 @@ leaner than `child_process.exec`. It has the same options.
333382
334383This is a special case of the ` spawn() ` functionality for spawning Node
335384processes. In addition to having all the methods in a normal ChildProcess
336- instance, the returned object has a communication channel built-in. The
337- channel is written to with ` child.send(message, [sendHandle]) ` and messages
338- are received by a ` 'message' ` event on the child.
339-
340- For example:
341-
342- var cp = require('child_process');
343-
344- var n = cp.fork(__dirname + '/sub.js');
345-
346- n.on('message', function(m) {
347- console.log('PARENT got message:', m);
348- });
349-
350- n.send({ hello: 'world' });
351-
352- And then the child script, ` 'sub.js' ` might look like this:
353-
354- process.on('message', function(m) {
355- console.log('CHILD got message:', m);
356- });
357-
358- process.send({ foo: 'bar' });
359-
360- In the child the ` process ` object will have a ` send() ` method, and ` process `
361- will emit objects each time it receives a message on its channel.
362-
363- There is a special case when sending a ` {cmd: 'NODE_foo'} ` message. All messages
364- containing a ` NODE_ ` prefix in its ` cmd ` property will not be emitted in
365- the ` message ` event, since they are internal messages used by node core.
366- Messages containing the prefix are emitted in the ` internalMessage ` event, you
367- should by all means avoid using this feature, it may change without warranty.
385+ instance, the returned object has a communication channel built-in. Se
386+ ` child.send(message, [sendHandle]) ` for details.
368387
369388By default the spawned Node process will have the stdout, stderr associated
370389with the parent's. To change this behavior set the ` silent ` property in the
@@ -373,31 +392,3 @@ with the parent's. To change this behavior set the `silent` property in the
373392These child Nodes are still whole new instances of V8. Assume at least 30ms
374393startup and 10mb memory for each new Node. That is, you cannot create many
375394thousands of them.
376-
377- The ` sendHandle ` option to ` child.send() ` is for sending a handle object to
378- another process. Child will receive the handle as as second argument to the
379- ` message ` event. Here is an example of sending a handle:
380-
381- var server = require('net').createServer();
382- var child = require('child_process').fork(__dirname + '/child.js');
383- // Open up the server object and send the handle.
384- server.listen(1337, function() {
385- child.send({ server: true }, server._handle);
386- });
387-
388- Here is an example of receiving the server handle and sharing it between
389- processes:
390-
391- process.on('message', function(m, serverHandle) {
392- if (serverHandle) {
393- var server = require('net').createServer();
394- server.listen(serverHandle);
395- }
396- });
397-
398- To close the IPC connection between parent and child use the
399- ` child.disconnect() ` method. This allows the child to exit gracefully since
400- there is no IPC channel keeping it alive. When calling this method the
401- ` disconnect ` event will be emitted in both parent and child, and the
402- ` connected ` flag will be set to ` false ` . Please note that you can also call
403- ` process.disconnect() ` in the child process.
0 commit comments