Strong-cluster-connect-store is an implementation of connect session store using node's native cluster messaging. It provides an easy way for using sessions in connect/express based applications running in a node cluster.
Features:
- Supports both Connect and Express.
- No dependencies on external services.
- Module is shipped without connect, it will use your version of connect or express.
- Covered by unit-tests.
See also API documentation.
$ npm install strong-cluster-connect-store
var connect = require('connect');
var ClusterStore = require('strong-cluster-connect-store')(connect);
var app = connect();
app
.use(connect.cookieParser())
.use(connect.session({ store: new ClusterStore(), secret: 'keyboard cat' }));
var express = require('express');
var ClusterStore = require('strong-cluster-connect-store')(express);
var app = express();
app
.use(express.cookieParser())
.use(express.session({ store: new ClusterStore(), secret: 'keyboard cat' }));
The store requires that a shared-state server is running in the master process.
The server is initialized automatically when you require() this module
from the master. In the case that your master and workers have separate source
files, you must explicitly require this module in your master source file.
Optionally, you can call setup()
to make it more obvious why you are loading
a module that is not used anywhere else.
// master.js
var cluster = require('cluster');
// etc.
require('strong-cluster-connect-store').setup();
// configure your cluster
// fork the workers
// etc.
}
The following example assumes you have set up Strong Cluster Connect Store for Express and have run the steps above.
'use strict';
var express = require('express');
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var ClusterStore = require('strong-cluster-connect-store')(express.session);
if (cluster.isMaster) {
// The cluster master executes this code
ClusterStore.setup();
// Create a worker for each CPU
for (var i=0; i<numCPUs; i++) {
cluster.fork();
}
cluster.on('online', function(worker) {
console.log('Worker ' + worker.id + ' is online.');
});
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.id + ' died with signal',signal);
});
} else {
// The cluster workers execute this code
var app = express();
app.use(express.cookieParser());
app.use(express.session(
{ store: new ClusterStore(), secret: 'super-cool' }
));
app.get('/hello', function(req, res) {
var msg;
if (req.session.visited)
msg = {msg: 'Hello again from worker '+cluster.worker.id};
else
msg = {msg: 'Hello from worker '+cluster.worker.id};
req.session.visited = '1';
res.json(200, msg);
});
app.listen(8080);
}