Sticky sessions for Node.js clustering done responsibly
This module provides sticky sessions for Node.js at the cluster level, routing requests to the underlying workers by producing hashes very quickly.
This module is an alternative to sticky-session, usually most useful when running Socket.IO >= 1.0.0
. Remember to also set up the socket.io-redis adapter!
This module is inspired on the suggestions outlined by elad in their node-cluster-socket.io
repository. All credit goes to them.
I built upon the idea by adding support for IPv6, and attempted to streamline the implementation as much as possible for the lipstick
consumers so that only minimal changes to your application are needed.
- Node.js >=
0.12.x
forpauseOnConnect
npm install lipstick --save
To use lipstick
, your master has to communicate with their workers effectively.
Here's a ready to use production-grade cluster.js
file. It will listen on port PORT
as defined in your environment variables. It will use app.js
as your worker process and spawn os.cpus().length
workers, or 2
of them, whichever is bigger. It will also route messages to the workers based on a hash of their IP address, applying the stickiness.
require('lipstick')();
The API for your cluster.js
module is detailed below.
The appfile
defaults to ./app.js
and will be used as the worker process. Options are detailed below.
Option | Description |
---|---|
port |
The port your application listens on |
workers |
The amount of workers your cluster should spawn |
You'll have to make a slight modification in your app.js
workers. Just change the following line:
app.listen(port, cb);
To the lipstick
equivalent shown below:
require('lipstick').listen(app, port, cb);
This will allow lipstick
to patch your worker processes. node app
will work as usual.
MIT