Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sticky-session with cluster in express - node js

I created a cluster depending app with reference to this question

But I started facing issues in session handling. how to use sticky-session in express js with cluster.

I was trying to use this npm module. But this resulted in the same situation. how to fix this session issue.

sticky(http.createServer(app).listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
}););
like image 879
Okky Avatar asked Jun 02 '14 09:06

Okky


1 Answers

Finally found solution just try this code. Its maintain sticky as well as it uses all the cpus [ process ] for other clients. You can use express cluster sticky session using following code. You can get sticky-session here https://github.com/indutny/sticky-session

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

app.get('/', function (req, res) {
    console.log('worker: ' + cluster.worker.id);
    res.send('Hello World!');
});


var server = http.createServer(app);
    sticky.listen(server,3000);
like image 162
lingeshram Avatar answered Oct 11 '22 17:10

lingeshram