Node.js,多线程和Socket.io [英] Node.js, multi-threading and Socket.io

查看:142
本文介绍了Node.js,多线程和Socket.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望让Socket.io能够使用本机负载平衡来实现多线程在Node.js v.0.6.0及更高版本中的(cluster)。

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.

据我所知,Socket.io使用Redis存储其内部数据。我的理解是:我们不想为每个工作者生成一个新的Redis实例,而是强制工作者使用与主服务器相同的Redis实例。因此,连接数据将在所有工作人员之间共享。

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.

在主人中这样的事情:

RedisInstance = new io.RedisStore;

我们必须以某种方式将 RedisInstance 传递给工作人员并执行以下操作:

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 completely in the wrong direction, anybody can point to some ideas?

推荐答案

实际上你的代码应该是这样的:

Actually 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);
    })
  });
}

另一种选择是打开Socket.IO来监听多个端口并拥有一些东西像HAProxy负载平衡的东西。
无论如何,你知道最重要的事情:使用RedisStore在流程外扩展!

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!

资源:

http://nodejs.org/docs/latest/api/cluster.html

如何扩展socket.io?

如何重用redis连接在socket.io中?

节点:缩放socket.io / nowjs - 跨不同实例进行扩展

http://delicious.com/alessioaw/socket.io

这篇关于Node.js,多线程和Socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆