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

socketio-sticky-session

v0.4.3

Published

Sticky session balancer with layer 4 capabilities, based on a `cluster` module

Downloads

1,326

Readme

Sticky session

A simple performant way to use socket.io with a cluster.

Technical

Sticky sessions are hash balanced by IP. Optionally, layer4 header information (HTTP) for reverse-proxied connections can be hashed.

Prolog to Proxied connections

If you're using a proxy, like you do in many constellations, e.g. using a varnish as cache, using a Cloudflare like CDN or using DDoS Protections which are build on a so called reverse proxy Server. There are many cases where you may not be able to avoid proxying the users requests, before they reach the node Application.

The Problem:

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.

Note: Only versions greater than 0.9.6 are supported.

Installation

npm install socketio-sticky-session

Configuration

You can optionally configure everything by the first parameter, by providing the following object:

var options = {
    num: integer,
    proxy: boolean,
    header: string,
    ignoreMissingHeader: boolean,
    sync: {
      isSyncable: boolean,
      event: string
    }
  };

num

Specifies the process count and is omittable. If omitted the core count of the processor will be used instead.

proxy

Specifies if the layer 4 patching should be used or not, needed if behind a proxy.

header

Specifies the header containing the real user IP and is omittable. If omitted the header defaults to x-forwarded-for. Also the header is case-insenstive.

ignoreMissingHeader

True will proxy even if the header is missing in a request. Needed for compatibility with some reverse proxies.

sync

Object containing information to manually call the sync of the initial packet and is also omittable. If omitted the behavior defaults to not syncing.

isSyncable

Specifies if sync is used or not.

event

Specifies on which event sticky-sessions should listen if isSyncable is set to true.

Note: The options parameter is omittable if you do not need it or can be a number to specify the process count (old call behavior).

Usage

Without proxied connections

var sticky = require('socketio-sticky-session');

sticky(require('http').createServer(function(req, res) {
  res.end('worker: ' + process.env.NODE_WORKER_ID);
})).listen(3000, function() {
  console.log('server started on 3000 port');
});

Simple

var sticky = require('socketio-sticky-session');

sticky(function() {
  // This code will be executed only in slave workers

  var http = require('http'),
      io = require('socket.io');

  var server = http.createServer(function(req, res) {
    // ....
  });
  io.listen(server);

  return server;
}).listen(3000, function() {
  console.log('server started on 3000 port');
});

Socket.io

With proxied connections

var sticky = require('socketio-sticky-session');

var options = {
  proxy: true, //activate layer 4 patching
  header: 'x-forwarded-for', //provide here your header containing the users ip
  num: 2 //count of processes to create, defaults to maximum if omitted
}

sticky(options, function() {
  // This code will be executed only in slave workers

  var http = require('http'),
      io = require('socket.io');

  var server = http.createServer(function(req, res) {
    // ....
  });
  io.listen(server);

  return server;
}).listen(3000, function() {
  console.log('server started on 3000 port');
});

Socket.io

var sticky = require('socketio-sticky-session');

var options = {
  proxy: true, //activate layer 4 patching
  header: 'x-forwarded-for', //provide here your header containing the users ip
  num: 2, //count of processes to create, defaults to maximum if omitted
  sync: {
    isSynced: true, //activate synchronization
    event: 'mySyncEventCall' //name of the event you're going to call
  }
}

var server = sticky(options, function() {
  // This code will be executed only in slave workers

  var http = require('http'),
      io = require('socket.io');

  var server = http.createServer(function(req, res) {
    // ....
  });
  io.listen(server);

  return server;
}).listen(3000, function() {
  console.log('server started on 3000 port');
});

server.on( 'connection', function( socket )
{
  // ... awesome stuff

  server.emit( 'mySyncEventCall' );
} );

Socket.io, synchronized

Reasoning

Socket.io is doing multiple requests to perform handshake and establish connection with a client. With a cluster those requests may arrive to different workers, which will break handshake protocol.

Sticky-sessions module is balancing requests using their IP address. Thus client will always connect to same worker server, and socket.io will work as expected, but on multiple processes!

LICENSE

This software is licensed under the MIT License.

Copyright Fedor Indutny, 2012.

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.