【问题标题】:sticky-session vs socket.io-sticky-session node js library ! Which is better?粘性会话与 socket.io-粘性会话节点 js 库!哪个更好?
【发布时间】:2016-04-06 14:42:08
【问题描述】:

我遇到过这两个用于 node js 的粘性会话库

https://github.com/indutny/sticky-session

https://github.com/wzrdtales/socket-io-sticky-session

两者有什么区别,我的需要只是实现带有节点集群的socket,以及将来如果我想添加Ngnx Server。

在 Socket.io 文档中他们提到了前一个,但是这个链接

Socket.io 'Handshake' failing with cluster and sticky-session

说第二个更好!

【问题讨论】:

  • 我知道这个问题可能看起来很垃圾,但作为一个初学者,我希望你能告诉我一些方法
  • 我在很长一段时间内也有同样的查询,但仍然不确定使用哪一个。
  • 据说If we proxy any connection, the real IP will be lost. The original implementation of sticky-sessions worked only on layer 3 of the OSI Model. But the information we need, is right now on layer 4。另一方面,indutny 是开发io.js 的人,所以他是一个真正值得信赖的人。如果需要,我会使用他的代码并切换到其他代码。
  • @AndreyPopov 请将其作为答案发布,以便我接受。
  • @AndreyPopov 我已经删除了我之前的评论,因为我已经找到了方法,我不知道每个工作线程都会调用 else 语句,在那里我可以连接我的 Moongoose 和套接字 io,谢谢你又来了

标签: node.js socket.io sticky-session


【解决方案1】:

最近发现sticky-cluster,这个模块实现起来非常简单。

基准测试非常好,他们的描述说:sticky-session 模块快 10 倍,散射效果更好

示例代码:

'use strict';
var sticky = require('sticky-cluster');

function startFn (callback) {
  var async = require('async');
  async.waterfall(
    [

      // connect to remote services

        function (callback) {
          async.parallel(
            [
              // fake db 1
              function (callback) { setTimeout(callback, 2000); },

              // fake db 2
              function (callback) { setTimeout(callback, 1000); }
            ],
            callback
          );
        },

      // configure the worker

        function (services, callback) {
          var http = require('http');
          var app = require('express')();
          var server = http.createServer(app);

          // get remote services
          //var fakedb1 = services[0];
          //var fakedb2 = services[1];

          // all express-related stuff goes here, e.g.
          app.use(function (req, res) { res.end('Handled by PID = ' + process.pid); });

          // all socket.io stuff goes here
          //var io = require('socket.io')(server);

          // don't do server.listen(...)!
          // just pass the server instance to the final async's callback
          callback(null, server);
        }

    ],
    function (err, server) {

      // fail on error
      if (err) { console.log(err); process.exit(1); }

      // pass server instance to sticky-cluster
      else callback(server);
    }
  );
}

sticky(startFn, {
  concurrency: parseInt(require('os').cpus().length, 10),
  port: parseInt(process.env.PORT, 10),
  debug: (process.env.NODE_ENV === 'development')
});

【讨论】:

猜你喜欢
  • 2014-07-16
  • 2012-05-16
  • 2012-01-03
  • 1970-01-01
  • 2011-09-16
  • 2019-06-21
  • 2019-09-24
  • 2016-07-25
相关资源
最近更新 更多