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

2

11

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'));
}););
Belshin answered 2/6, 2014 at 9:34 Comment(1)
Your question was a good one. The sticky-session module maintainers should update their readme with examples of both basic http server usage, and integrating with express module.Horsewoman
T
4

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);
Tremblay answered 11/6, 2016 at 22:41 Comment(0)
E
-4

It has nothing to do with Express.

You just forgot the listen() on the sticky function.

sticky(
  http.createServer(app).listen(app.get('port'), function () {
      console.log('Express server listening on port ' + app.get('port'));
  });
).listen(app.get('port'),function() {
  console.log('Sticky server started on port' + app.get('port'));
});
Evitaevitable answered 30/10, 2014 at 22:28 Comment(1)
The above doesn't work, and you shouldn't be listening twice. Better examples though still not working as expected: github.com/indutny/sticky-session/issues/7Horsewoman

© 2022 - 2024 — McMap. All rights reserved.