feeco
feeco

Reputation: 135

should I create multiple websocket instance or using noserver mode when I want to separate websocket cononections?

as a node express backend. I need a websocket of real-time chat, and a websocket of go game for multiple players. I use https://github.com/websockets/ws library. there are two solutions:

1.create multiple websocket server instances by passing different port.for example:

let chatServer = new WebsocketServer({port:8081})
let goServer = new WebsocketServer({port:8082})

from front end, different page useEffect connect to different websocket server.

2.Multiple servers sharing a single HTTP/S server as ws documentation suggested.

const server = createServer();
const wss1 = new WebSocketServer({ noServer: true });
const wss2 = new WebSocketServer({ noServer: true });

wss1.on('connection', function connection(ws) {
  // ...
});

wss2.on('connection', function connection(ws) {
  // ...
});

from front end, connect to different server by pathname.

My question is, which one I should choose, and what is the difference? I really really want to know, what does "noServer" mode mean. thanks.

Upvotes: 2

Views: 330

Answers (0)

Related Questions