Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nginx Reverse proxying / Multiple HTTP servers #47

Closed
Tom-Bonnike opened this issue Jun 20, 2016 · 4 comments
Closed

Nginx Reverse proxying / Multiple HTTP servers #47

Tom-Bonnike opened this issue Jun 20, 2016 · 4 comments

Comments

@Tom-Bonnike
Copy link

Tom-Bonnike commented Jun 20, 2016

Hi.
My issue is summed up here: http://stackoverflow.com/questions/37911155/nodejs-clusters-socket-io-redis-nginx
I am aware of issue #24 , issue #6 and PR #45 , but they sadly haven't been answered.

What I want to know is if what I'm trying to do is even possible? If it isn't, what should I do instead?
Wouldn't just allowing the websockets transport be a good solution to avoid the multiple requests?

@Mad-Head
Copy link

I think you should use https://github.com/indutny/sticky-session with https://github.com/socketio/socket.io-redis

Something like this:

const cluster = require('cluster');
const sticky = require('sticky-session');
const io_redis = require('socket.io-redis');

const port = process.env.PORT || 3000;
const http = require('http');
const app = require('../app');
const server = http.createServer(app);
const io = require('socket.io')(server);

io.adapter(io_redis({ host: 'localhost', port: 6379 }));

if (!sticky.listen(server, port)) {
    server.once('listening', function() {
        console.log('server started on port: ' + port);
    });

    cluster.on('online', (worker) => {
        console.log('worker ' + worker.id + ' pid ' + process.pid + ' started');
    });

    cluster.on('disconnect', (worker) => {
        console.log(`The worker #${worker.id} has disconnected`);
    });

    cluster.on('exit', (worker, code, signal) => {
        console.log('worker %d died (%s). restarting...', worker.process.pid, signal || code);
    });
} else {
    require('../sockets/server')(io);
}

Of course, you should install redis server. http://redis.io/download

@v4l3r10
Copy link

v4l3r10 commented Jun 21, 2016

I can confirm the suggestion of @Mad-Head . I use the same approch in a production env and works very well!

@Tom-Bonnike
Copy link
Author

Tom-Bonnike commented Jun 21, 2016

Yes, but the problem is that sticky session doesn't work with proxies (as it uses IP to transfer the requests to the right cluster, it would always show 127.0.0.1 as the IP). I could be mistaken though, and I haven't even tried because I actually just solved my problem.

I decided to drop the longpolling transport for socket.io and now only use websocket transport, which only needs 1 request to make the initial connection. This means it won't work on some platforms, but I don't really care for my project. So I don't need to use sticky-sessions anymore, or any other code that would help me retrieve and store the real IP of the original request.

Thanks anyway to you two!
If you feel like this is wrong, feel free to explain why! :)

@Mad-Head
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants