【问题标题】:Scaling to 2+ dynos on Heroku with socket.io-redis and RedisToGo使用 socket.io-redis 和 RedisToGo 在 Heroku 上扩展到 2+ dynos
【发布时间】:2015-05-04 17:19:24
【问题描述】:

我正在尝试使用 socket.io-redis 将我在 Heroku 上的应用程序扩展到 2 dynos(或更多)。这是我的代码(其中 config.redis 只是一个包含 RedisToGo 端口、主机和传递值的对象):

var redisApp = require('redis');
var redis = require('socket.io-redis');    
if(process.env.NODE_ENV === 'production') {
   var socketpub = redisApp.createClient(config.redis.port, config.redis.host, {auth_pass: config.redis.pass, return_buffers: true});
   var socketsub = redisApp.createClient(config.redis.port, config.redis.host, {auth_pass: config.redis.pass, detect_buffers: true});
   var client = redisApp.createClient(config.redis.port, config.redis.host, {auth_pass: config.redis.pass, return_buffers: true});
   socketio.adapter(redis({
      pubClient: socketpub,
      subClient: socketsub,
      redisClient: client
   }));
}

在客户端我有:

var ioSocket = io('', {
  path: '/socket.io-client',
  'force new connection': true,
  transports: ['websocket']
});

..so socket.io 不会尝试使用轮询。

我还为 RedisToGo (REDISTOGO_HOST, REDISTOGO_PASS,REDISTOGO_PORT)。

当我们缩放到 1 dyno 时,套接字行为是完美的。在 2 个 dyno 处,行为偏离了 - 请求被随机发送到 1 个 dyno 或另一个,并且发出的套接字事件仅发送到在发出请求的 dyno 上运行的客户端,而不是全部(哪个套接字.io-redis 和 RedisToGo 应该照顾)。

任何想法将不胜感激!

【问题讨论】:

    标签: node.js sockets heroku redistogo socket.io-redis


    【解决方案1】:

    不确定这是否对您有帮助,但我正在以这种方式使用 redis 和 socketio,并且效果很好。

    var redis = require('redis').createClient;
    var adapter = require('socket.io-redis');
    var port = config.redistogo.port;
    var host = config.redistogo.host;
    
    var pub = redis(port, host, {
      auth_pass: auth_pass
    });
    
    var sub = redis(port, host, {
      detect_buffers: true,
      auth_pass: auth_pass
    });
    
    io.adapter(adapter({
      pubClient: pub,
      subClient: sub
    }));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-25
      • 2012-06-30
      • 2012-08-16
      • 2012-06-19
      • 2016-03-16
      • 1970-01-01
      • 2019-01-18
      • 2012-05-22
      相关资源
      最近更新 更多