https://github.com/strongloop/strong-cluster-connect-store
Implementation of connect store using node's native cluster messaging
https://github.com/strongloop/strong-cluster-connect-store
Last synced: 6 months ago
JSON representation
Implementation of connect store using node's native cluster messaging
- Host: GitHub
- URL: https://github.com/strongloop/strong-cluster-connect-store
- Owner: strongloop
- License: other
- Created: 2013-07-09T19:09:23.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2017-01-27T14:47:51.000Z (about 9 years ago)
- Last Synced: 2025-05-02T10:50:49.826Z (9 months ago)
- Language: JavaScript
- Size: 43 KB
- Stars: 11
- Watchers: 24
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Connect Session Store for Cluster
[](https://travis-ci.org/strongloop/strong-cluster-connect-store)
[](http://badge.fury.io/js/strong-cluster-connect-store)
## 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:
- 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](http://apidocs.strongloop.com/strong-cluster-connect-store/).
## Installation
```sh
$ npm install strong-cluster-connect-store
```
## Use
### Configuration - connect
```js
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
```javascript
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.
```javascript
// 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.
```javascript
'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