简体   繁体   中英

Socket.io / Express with load balancers servers

I am doing a socket.io integration on Express server, and the frontend (React) connects to the server.

To avoid request overload, load balancers have been configured as intermediaries before reaching the server where the Express server is deployed.

My question is, will the front be able to connect to the Express server / socket.io, which flow goes through the load balancers? What I read was that the transport mode should be " websockets " and not " polling ".

Thank you very much for your advice.

f you force the webSocket transport so socket.io does not initially start out with polling mode (like it usually does) and if the load balancers support webSocket connections, it all should work. The other way to make polling work with load balancers is to make the load balancers sticky so that a given client is load balanced to the same server each time.

Note, that if your app has reasons to want a client to stay on the same load-balanced server (like supposed it gets momentarily disconnected and then automatically reconnects), then you would want your load balancers to be configured as sticky. If that doesn't matter, then just forcing the websocket transport initially should be enough.

FYI, here's how you force the socket.io client to start with a webSocket connection:

const socket = io({transports: ["websocket"]});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM