本文介绍了sticky-session vs socket.io-sticky-session节点js库!哪个更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了两个用于节点js的粘性会话库

I have come across this two sticky-session library for node js

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

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

两者之间有什么区别,我只需要实现带有节点群集的套接字,如果将来我想添加Ngnx Server,也可以实现.

What is the difference between two, My need is just to achieve socket with node clusters, and also in future if i want to add Ngnx Server.

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

In Socket.io Documentation they have mentioned about the former one, But this link

Socket.io握手"失败,群集和粘性-会话

说第二个更好!

推荐答案

最近,我发现了 sticky-cluster ,并且该模块的实现非常简单.

Recently, I found sticky-cluster and this module has a very easy implementation.

基准测试非常好,描述如下:速度比sticky-session模块快10倍,并且散射效果好得多.

The benchmarking is very good and their description says: up to 10x faster and with much better scattering than sticky-session module.

示例代码:

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

这篇关于sticky-session vs socket.io-sticky-session节点js库!哪个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 03:47