This is a special case of the `spawn()` functionality for spawning Node
processes. In addition to having all the methods in a normal ChildProcess
instance, the returned object has a communication channel built-in. The
-channel is written to with `child.send(message)` and messages are recieved
-by a `'message'` event on the child.
+channel is written to with `child.send(message, [sendStream])` and messages
+are recieved by a `'message'` event on the child.
For example:
startup and 10mb memory for each new Node. That is, you cannot create many
thousands of them.
+The `sendStream` option to `child.send()` is for sending a `net.Socket`
+or `net.Server` object to another process. Child will receive the handle as
+as second argument to the `message` event.
+
### child.kill(signal='SIGTERM')
var jsonBuffer = '';
- channel.onread = function(pool, offset, length) {
+ channel.onread = function(pool, offset, length, recvStream) {
if (pool) {
for (var i = 0; i < length; i++) {
if (pool[offset + i] === LF) {
jsonBuffer = pool.toString('ascii', i, length);
offset = i + 1;
- target.emit('message', message);
+ target.emit('message', message, recvStream);
}
}
} else {
}
};
- target.send = function(message, fd) {
- if (fd) throw new Error("not yet implemented");
-
+ target.send = function(message, sendStream) {
if (!target._channel) throw new Error("channel closed");
+ // Open up net.Socket instances
+ if (sendStream instanceof require('net').Socket) {
+ sendStream = sendStream._handle;
+ }
+
// For overflow protection don't write if channel queue is too deep.
if (channel.writeQueueSize > 1024 * 1024) {
return false;
var buffer = Buffer(JSON.stringify(message) + '\n');
- var writeReq = channel.write(buffer);
+ var writeReq = channel.write(buffer, 0, buffer.length, sendStream);
if (!writeReq) {
throw new Error(errno + " cannot write to IPC channel.");
} else {
uv_stream_t* send_stream = NULL;
- if (args.Length() > 3) {
- assert(args[3]->IsObject());
+ if (args[3]->IsObject()) {
Local<Object> send_stream_obj = args[3]->ToObject();
assert(send_stream_obj->InternalFieldCount() > 0);
StreamWrap* send_stream_wrap = static_cast<StreamWrap*>(