admin管理员组文章数量:1242806
I'm looking to get Socket.io to work multi-threaded with native load balancing ("cluster") in Node.js v.0.6.0 and later.
From what I understand, Socket.io uses Redis to store its internal data. My understanding is this: instead of spawning a new Redis instance for every worker, we want to force the workers to use the same Redis instance as the master. Thus, connection data would be shared across all workers.
Something like this in the master:
RedisInstance = new io.RedisStore;
The we must somehow pass RedisInstance
to the workers and do the following:
io.set('store', RedisInstance);
Inspired by this implementation using the old, 3rd party cluster module, I have the following non-working implementation:
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
var sio = require('socket.io')
, RedisStore = sio.RedisStore
, io = sio.listen(8080, options);
// Somehow pass this information to the workers
io.set('store', new RedisStore);
} else {
// Do the work here
io.sockets.on('connection', function (socket) {
socket.on('chat', function (data) {
socket.broadcast.emit('chat', data);
})
});
}
Thoughts? I might be going pletely in the wrong direction, anybody can point to some ideas?
I'm looking to get Socket.io to work multi-threaded with native load balancing ("cluster") in Node.js v.0.6.0 and later.
From what I understand, Socket.io uses Redis to store its internal data. My understanding is this: instead of spawning a new Redis instance for every worker, we want to force the workers to use the same Redis instance as the master. Thus, connection data would be shared across all workers.
Something like this in the master:
RedisInstance = new io.RedisStore;
The we must somehow pass RedisInstance
to the workers and do the following:
io.set('store', RedisInstance);
Inspired by this implementation using the old, 3rd party cluster module, I have the following non-working implementation:
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
var sio = require('socket.io')
, RedisStore = sio.RedisStore
, io = sio.listen(8080, options);
// Somehow pass this information to the workers
io.set('store', new RedisStore);
} else {
// Do the work here
io.sockets.on('connection', function (socket) {
socket.on('chat', function (data) {
socket.broadcast.emit('chat', data);
})
});
}
Thoughts? I might be going pletely in the wrong direction, anybody can point to some ideas?
Share Improve this question asked Dec 19, 2011 at 15:18 David ChouinardDavid Chouinard 6,8468 gold badges46 silver badges64 bronze badges 1- 1 I wonder what approach you end up using. – mvbl fst Commented Jun 3, 2013 at 19:04
1 Answer
Reset to default 11Actually your code should look like this:
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
var sio = require('socket.io')
, RedisStore = sio.RedisStore
, io = sio.listen(8080, options);
// Somehow pass this information to the workers
io.set('store', new RedisStore);
// Do the work here
io.sockets.on('connection', function (socket) {
socket.on('chat', function (data) {
socket.broadcast.emit('chat', data);
})
});
}
Another option is to open Socket.IO to listen on multiple ports and have something like HAProxy load-balance stuff. Anyway you know the most important thing: using RedisStore to scale outside a process!
Resources:
http://nodejs/docs/latest/api/cluster.html
How can I scale socket.io?
How to reuse redis connection in socket.io?
Node: Scale socket.io / nowjs - scale across different instances
http://delicious./alessioaw/socket.io
本文标签: JavaScriptNodeJSmultithreading and SocketioStack Overflow
版权声明:本文标题:javascript - Node.js, multi-threading and Socket.io - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740157181a2233755.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论