@@ -55,7 +55,7 @@ An alternative way to check if you can send messages is to see if the
55
55
### Event: 'message'
56
56
57
57
* ` message ` {Object} a parsed JSON object or primitive value
58
- * ` sendHandle ` {Handle object} a handle object
58
+ * ` sendHandle ` {Handle object} a Socket or Server object
59
59
60
60
Messages send by ` .send(message, [sendHandle]) ` are obtained using the
61
61
` message ` event.
@@ -129,7 +129,7 @@ See `kill(2)`
129
129
* ` message ` {Object}
130
130
* ` sendHandle ` {Handle object}
131
131
132
- When useing ` child_process.fork() ` an you can write to the child using
132
+ When using ` child_process.fork() ` you can write to the child using
133
133
` child.send(message, [sendHandle]) ` and messages are received by
134
134
a ` 'message' ` event on the child.
135
135
@@ -162,9 +162,73 @@ the `message` event, since they are internal messages used by node core.
162
162
Messages containing the prefix are emitted in the ` internalMessage ` event, you
163
163
should by all means avoid using this feature, it is subject to change without notice.
164
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.
165
+ The ` sendHandle ` option to ` child.send() ` is for sending a TCP server or
166
+ socket object to another process. The child will receive the object as its
167
+ second argument to the ` message ` event.
168
+
169
+ ** send server object**
170
+
171
+ Here is an example of sending a server:
172
+
173
+ var child = require('child_process').fork('child.js');
174
+
175
+ // Open up the server object and send the handle.
176
+ var server = require('net').createServer();
177
+ server.on('connection', function (socket) {
178
+ socket.end('handled by parent');
179
+ });
180
+ server.listen(1337, function() {
181
+ child.send('server', server);
182
+ });
183
+
184
+ And the child would the recive the server object as:
185
+
186
+ process.on('message', function(m, server) {
187
+ if (m === 'server') {
188
+ server.on('connection', function (socket) {
189
+ socket.end('handled by child');
190
+ });
191
+ }
192
+ });
193
+
194
+ Note that the server is now shared between the parent and child, this means
195
+ that some connections will be handled by the parent and some by the child.
196
+
197
+ ** send socket object**
198
+
199
+ Here is an example of sending a socket. It will spawn two childs and handle
200
+ connections with the remote address ` 74.125.127.100 ` as VIP by sending the
201
+ socket to a "special" child process. Other sockets will go to a "normal" process.
202
+
203
+ var normal = require('child_process').fork('child.js', ['normal']);
204
+ var special = require('child_process').fork('child.js', ['special']);
205
+
206
+ // Open up the server and send sockets to child
207
+ var server = require('net').createServer();
208
+ server.on('connection', function (socket) {
209
+
210
+ // if this is a VIP
211
+ if (socket.remoteAddress === '74.125.127.100') {
212
+ special.send('socket', socket);
213
+ return;
214
+ }
215
+ // just the usual dudes
216
+ normal.send('socket', socket);
217
+ });
218
+ server.listen(1337);
219
+
220
+ The ` child.js ` could look like this:
221
+
222
+ process.on('message', function(m, socket) {
223
+ if (m === 'socket') {
224
+ socket.end('You where handled as a ' + process.argv[2] + ' person');
225
+ }
226
+ });
227
+
228
+ Note that once a single socket has been sent to a child the parent can no
229
+ longer keep track of when the socket is destroyed. To indicate this condition
230
+ the ` .connections ` property becomes ` null ` .
231
+ It is also recomended not to use ` .maxConnections ` in this condition.
168
232
169
233
### child.disconnect()
170
234
@@ -382,7 +446,7 @@ leaner than `child_process.exec`. It has the same options.
382
446
383
447
This is a special case of the ` spawn() ` functionality for spawning Node
384
448
processes. In addition to having all the methods in a normal ChildProcess
385
- instance, the returned object has a communication channel built-in. Se
449
+ instance, the returned object has a communication channel built-in. See
386
450
` child.send(message, [sendHandle]) ` for details.
387
451
388
452
By default the spawned Node process will have the stdout, stderr associated
0 commit comments