strongloop / strong-cluster-connect-store

Implementation of connect store using node's native cluster messaging
Other
11 stars 3 forks source link

Connect Session Store for Cluster

Build Status NPM version

Overview

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:

See also API documentation.

Installation

$ npm install strong-cluster-connect-store

Use

Configuration - connect

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' }));

Configuration - express

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' }));

Setting up the master process

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.
}

Using Strong Cluster Connect Store

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);
}