Here's my server code:
I'm trying to use cluster with socket.io and expressjs. I'm testing this on my quad core desktop.
var cluster = require('cluster')
var numCPUs = require('os').cpus().length
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork()
}
console.log(new Date());
}
else {
// get required modules
var connect = require('connect');
var express = require('express');
var mongo = require('mongodb');
var MongoStore = require('connect-mongo')(express);
var server;
var redis = require('redis');
var http = require('http');
// create server object
app = exports.module = express();
function configureServer() {
//configure environments
app.configure('production', function() {
app.use(express.errorHandler());
});
//configure server instance
app.configure(function() {
app.use(express.compress())
// set jade as default view engine
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.cookieParser("refdgfd"));
app.use(express.methodOverride());
app.use(app.router);
// use express logger
});
loadModules();
}
var server = http.createServer(app).listen(80);
socket = require('socket.io').listen(server);
var RedisStore = require('socket.io/lib/stores/redis')
, pub = redis.createClient()
, sub = redis.createClient()
, client = redis.createClient();
socket.set('store', new RedisStore({
redisPub : pub
, redisSub : sub
, redisClient : client
}));
socket.configure(function() {
socket.set('log level', 1);
socket.set('transports', ['websocket']);
});
configureServer();
}
Is there anything that stands out to be really wrong?
The logs indicate that everything happens 4 times. Which seems correctly. But my socket.io's channel users also indicate that whenever I connect to a page, I connect 4 times, (create 4 user sockets). That seems wrong. How do I fix it and are there other things wrong with my code. Thanks.
At this moment socket.io
has no full support for node.js cluster
. It works fine for me with websocket
transport, but xhr-pooling
fails. You can reed more in this issues: #939, #917. The problem is the xhr-pooling
request can get into the thread different from the one which it was originally authorized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With