http server and web sockets from separate servers

2019-07-17 19:48发布

It's pretty easy to configure a http server (using express) and a socket server (socket.io) assigned to it:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

How can I run http server and socket server in two different node.js instances?

My idea is to leverage the performance this way, releasing the http node instance from the responsibility of also sending notifications back to the clients.

1条回答
SAY GOODBYE
2楼-- · 2019-07-17 20:32

In a regular Socket.IO + Express app, Socket.IO intercepts requests starting with /socket.io/.

You may set Nginx (or any other webserver that supports proxying) listening 80 port, and make it proxy to Socket.IO process if request starts with /socket.io/, and to Express process otherwise.

Edit: To set up Socket.IO in separate process you may use the following code:

var io = require('socket.io')();
io.on('connection', function(socket){
    //here you can emit and listen messages
});
io.listen(3000);
查看更多
登录 后发表回答