3

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

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.

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

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

says that second one is better !

Community
  • 1
  • 1
Bikash
  • 1,452
  • 1
  • 15
  • 24
  • I know this questions may look dump, But as i am a beginner, I would want you to just show me some ways to do it – Bikash Apr 06 '16 at 14:54
  • 2
    I am also having the same query from quite some time, still not sure which one to use. – jain Apr 06 '16 at 16:47
  • 1
    It's said that `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`. On the other hand, `indutny` is the guy that developed `io.js`, so he's a truly trusted one. I would go with his code and switch to the other **only** if I need to. – Andrey Popov Apr 07 '16 at 07:25
  • @AndreyPopov Please post it as answer so that i can accept it. – Bikash Apr 07 '16 at 10:09
  • @AndreyPopov I have deleted my previous comment, as i have found out the way, i was not aware that else statement get called for every worker thread, there i can connect my Moongoose and socket io , thank you once again – Bikash Apr 07 '16 at 11:26

1 Answers1

3

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

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

Example code:

'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')
});
saeta
  • 4,048
  • 2
  • 31
  • 48
  • 1
    Hi, I just can't make it work with pm2. Do you have any experience with that? – Qiulang Oct 24 '17 at 03:15
  • @usama check my question here https://stackoverflow.com/questions/46891819/how-to-make-sticky-session-works-with-socket-io-w-or-w-o-pm2 – Qiulang Jun 12 '19 at 02:09