npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-sticky-cluster

v1.3.5

Published

Sticky session balancer based on a cluster and node-http-proxy modules, with express and socket.io support

Downloads

78

Readme

express-sticky-cluster

Sticky session balancer based on a cluster and node-http-proxy modules, with express and socket.io support.

Designed for using like express-cluster.

No memory leaks since version 1.0.0!

NPM

How it works

Launches multiple worker processes through cluster, using bunch of ports. One worker process becomes also 'http-proxy', serving as sticky session balancer.

Establishes sticky round-robin balancer for any kind of http(s) frameworks. Not only, but including socket.io and express. Client will always connect to same worker process sticked with customizable hash function. For example, socket.io multi-stage authorization will work as expected.

Unused sessions stickers will be deleted after some time, so module does not have memory leaks.

Installation

npm install express-sticky-cluster [--save]

Usage example

Express 4 example with socket.io and passport.js usage.

./bin/www:

var app = require('../worker'),
    debug = require('debug')('example-app:server'),
    https = require('https'),
    fs = require('fs'),
    ipfilter = require('express-ipfilter').IpFilter;
var express_sticky_cluster = require('express-sticky-cluster');

var optionsHTTPs = {
        key: fs.readFileSync('./cert/def-secured.key'),
        cert: fs.readFileSync('./cert/def-secured.crt')
    };
    
var server = https.createServer(optionsHTTPs, app);
require('../socket.io')(require('socket.io')(server));

express_sticky_cluster(
    {
	    workers: 0,
	    respawn: true,
	    socket: true,
	    proxy_port: 443,
        worker_port: 8000,
        delay: 1000,        	    
        debug: true,
        ssl: {
	        secure: true,
	        certs: optionsHTTPs
        },	  
        ipfilter: ipfilter(['127.0.0.1', ['192.168.0.1', '192.168.0.200']], {mode: allow, log: false}),
	    session: {
            hash: 'connect.sid',
            ttl: 360000
	    },
	    logger: undefined,
	    workerListener: function (message) {}
	}, function (worker, port) {
	    console.info('Worker ID#' + worker.id + ' process started PID#' + worker.process.pid);
	    app.set('port', port);
	    return server.listen(port);
	}
);

server.on('error', onError);
server.on('listening', onListening);

function onError(error) {
    if (error.syscall !== 'listen')
        throw error;
    var bind = typeof port === 'string'
        ? 'Pipe ' + port
        : 'Port ' + port;
    switch (error.code) {
        case 'EACCES':
            console.error(bind + ' requires elevated privileges');
            process.exit(1);
            break;
        case 'EADDRINUSE':
            console.error(bind + ' is already in use');
            process.exit(1);
            break;
        default:
            throw error;
    }
}
function onListening() {
    var addr = server.address();
    var bind = typeof addr === 'string'
        ? 'pipe ' + addr
        : 'port ' + addr.port;
    console.info('Listening on ' + bind + ' PID#' + process.pid);
}

./worker.js:

var express = require('express'),
    session = require('express-session'),
    passport = require('passport'),
	uuid = require('node-uuid');
	
var app = express();

var sessionFileStore = require('session-file-store')(session);
global.sessionStore = new sessionFileStore({
    path : path.join(__dirname, '/sessions')
});    
...
require('./passport')(passport);
app.use(passport.initialize());
app.use(passport.session());
...
app.use(session({
    secret: 'keyboard-cat',
    name: 'connect.sid',
    resave: true,
    rolling: true,
    saveUninitialized: true,
    cookie: {
        path: '/',
        httpOnly: false,
        secure: true,
        maxAge: 360000
    },
    store: global.sessionStore,
    genid: function (req) {
        return uuid.v4();
    }
}));
...
module.exports = app;

./socket.io.js:

var passportIo = require('passport.socketio'),
    cookieParser = require('cookie-parser');

module.exports = function (io) {
    io.use(passportIo.authorize({
        cookieParser: cookieParser,
        key: 'connect.sid',
        secret: 'keyboard-cat',
        store: global.sessionStore,
        success: function (data, accept) {
            accept();
        },
        fail: function (data, message, err, accept) {
            if(err)
                accept(new Error(message));
        }
    }));
    io.on('connection', function (socket) {
        socket.on('join', function (data) {
            console.log('join', data);
        });
        socket.on('disconnect', function () {
            socket.leave();
        });
    });
};

./passport.js:

var LocalStrategy = require('passport-local').Strategy;

module.exports = function (passport, licensing) {
    passport.serializeUser(function (user, done) {
        done(null, user);
    });
    passport.deserializeUser(function (user, done) {
        done(null, user);
    });
    passport.use(
        'local-signin',
        new LocalStrategy({
                usernameField: 'username',
                passwordField: 'password',
                passReqToCallback: true
            },
            function (req, username, password, done) {
                var objUser = {};
                return done(null, objUser, {message: 'Successfull login'});
            }
        )
    );
};

Options

  • workers - Number of workers to spawn (default: CPU core count)
  • respawn - Respawn process on exit (default: true)
  • socket - Allow WebSocket proxy (default: true)
  • proxy_port - Proxy listener port (default: if secure then 443 else 80)
  • worker_port - Workers first port, increased for each worker (for 4 workers it will be use [worker_port, worker_port+1, worker_port+2, worker_port+3]) (default: 8000)
  • delay - Proxy instance creating delay (default: 1000ms)
  • debug - Log Proxy debug info to logger (default: false)
  • ssl:
    1. secure - Create HTTP or HTTPS proxy (default: false means HTTP)
    2. certs - TLS certificates data object (default: undefined, if secure=true must contain you TLS certificates data)
  • ipfilter - IP-filter function, for details see express-ipfilter (default: undefined)
  • session:
    1. hash - Function (req, res) or Session cookie name. If function - can use cookie-based session ids and etc. (default: if undefined uses cookie-based session id from cookie 'connect.sid')
    2. ttl - Sessions TTL, uses for store configuration (default: 3600000ms)
  • logger - Logger instance, for example new winston.Logger(...) instance (default: undefined)
  • workerListener - Attach the given function to each spawned worker. The function will be bound to the worker that sent the message so you can setup a two way message bus if you want (default: undefined)

Callback function

  • worker - current worker instance
  • port - current worker assigned port

LICENSE

MIT License

Copyright (c) 2016 Valeriy V. Pushkar and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.