I am using Socket.io and clustering. What I need to do is be able to make multiple child processes and send specific [socket.io] sockets to each one as I see fit. Currently (see code below) when I attempt to send the [socket.io] socket as a handle I get an exception.
My code below doesn't have it (as I am trying to get the most basic example to work) but what I want to do is allow a client to connect and tell it what process to go to upon some message. So a client will send an 'init' message with some data and based on that data I forward the [socket.io] socket to a specific process. This was my original plan.
I know I can have socket.io listen from every child process however when a client connects only one of those processes receive it.
My two questions are:
- Is there a way to send a socket.io socket to a child process?
- If not, is there another way to decide which process gets the handle? (Could you use a different authorization function in each process and if one process doesn't accept the socket another process could?)
var cluster = require('cluster');
if (cluster.isMaster) {
var io = require('socket.io').listen(80);
var cpuCount = require('os').cpus().length;
var children = [];
for (var i = 0; i < cpuCount; i += 1) {
children.push(cluster.fork());
}
//Master Process is listening for all connections
io.sockets.on('connection', function (socket) {
//upon a connection, send the 'handle' to a worker.
children[0].send('server', socket);
});
} else {
process.on('message', function (m, handle) {
//worker receives it here.
console.log('here');
});
}
Console output with stack trace:
C:\Users\randy>node "C:\Users\randy\Documents\Visual Studio 2012\Projects\tesselconnect-server\tesselconnect-server\server.js"
info - socket.io started
debug - client authorized
info - handshake authorized bZCM2CVpFFdU9eU1zYwx
debug - setting request GET /socket.io/1/websocket/bZCM2CVpFFdU9eU1zYwx
debug - set heartbeat interval for client bZCM2CVpFFdU9eU1zYwx
debug - client authorized for
debug - websocket writing 1::
child_process.js:427
throw new TypeError("This handle type can't be sent");
^
TypeError: This handle type can't be sent
at ChildProcess.target.send (child_process.js:427:15)
at Worker.send (cluster.js:401:21)
at SocketNamespace.<anonymous> (C:\Users\randy\Documents\Visual Studio 2012\Projects\tesselconnect-server\tesselconnect-server\server.js:16:15)
at SocketNamespace.EventEmitter.emit [as $emit] (events.js:117:20)
at connect (C:\Users\randy\node_modules\socket.io\lib\namespace.js:292:10)
at C:\Users\randy\node_modules\socket.io\lib\namespace.js:308:13
at SocketNamespace.authorize (C:\Users\randy\node_modules\socket.io\lib\namespace.js:252:5)
at SocketNamespace.handlePacket (C:\Users\randy\node_modules\socket.io\lib\namespace.js:302:14)
at Manager.handleClient (C:\Users\randy\node_modules\socket.io\lib\manager.js:698:32)
at Manager.handleUpgrade (C:\Users\randy\node_modules\socket.io\lib\manager.js:618:8)
Line 16:
children[0].send('server', socket);
net.createServer(function (socket) { });. So, I am going to assume that this is not possible using socket.io.