# sticky-session

> Sticky session balancer based on a `cluster` module

Latest version **1.1.2** (published 2016-06-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install sticky-session
pnpm add sticky-session
yarn add sticky-session
bun add sticky-session
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2016-06-01 |
| First published | 2012-02-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/sticky-session) |
| Module format | CommonJS |
| Node | >= 0.12.0 |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Fedor Indutny |
| Maintainers | fedor.indutny, indutny, tlhunter |

## Links

- npm: https://www.npmjs.com/package/sticky-session
- npm.io page: https://npm.io/package/sticky-session

## Dependencies (2)

- [ip](https://npm.io/package/ip.md) ^1.0.0
- [debug](https://npm.io/package/debug.md) ^2.2.0

## Recent versions

- 1.1.2 (latest) — 2016-06-01
- 0.0.5 (stable) — 2012-02-18
- 1.1.1 — 2016-06-01
- 1.1.0 — 2016-06-01
- 1.0.3 — 2016-06-01
- 1.0.2 — 2015-10-10
- 1.0.1 — 2015-09-26
- 1.0.0 — 2015-09-05
- 0.1.0 — 2013-03-14
- 0.0.4 — 2012-02-16
- 0.0.3 — 2012-02-16
- 0.0.2 — 2012-02-15

## README

# Sticky session

A simple performant way to use [socket.io][0] with a [cluster][1].

## Installation

```bash
npm install sticky-session
```

## Usage

```javascript
var cluster = require('cluster'); // Only required if you want the worker id
var sticky = require('sticky-session');

var server = require('http').createServer(function(req, res) {
  res.end('worker: ' + cluster.worker.id);
});

if (!sticky.listen(server, 3000)) {
  // Master code
  server.once('listening', function() {
    console.log('server started on 3000 port');
  });
} else {
  // Worker code
}
```
Simple

## 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!

#### Note about `node` version

`sticky-session` requires `node` to be at least `0.12.0` because it relies on
`net.createServer`'s [`pauseOnConnect` flag][2].

A deeper, step-by-step explanation on how this works can be found in
[`elad/node-cluster-socket.io`][3]

#### LICENSE

This software is licensed under the MIT License.

Copyright Fedor Indutny, 2015.

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.

[0]: http://socket.io/
[1]: http://nodejs.org/docs/latest/api/cluster.html
[2]: https://nodejs.org/api/net.html#net_net_createserver_options_connectionlistener
[3]: https://github.com/elad/node-cluster-socket.io

---
_Source: https://npm.io/package/sticky-session · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
