Socket.io handshake fails on Google https load balancer

google-cloud-platformgoogle-compute-enginehttpssocketsocket.io

We have the following setup for Google HTTPS load balancer.

Two Frontends:
1. HTTP traffic to static IP
2. HTTPS traffic to the same static IP(DNS configured to a domain name)

Host and Path rules
All going to backend

One Backend:
With HTTP protocol with session affinity set to client IP

The backend instance has a MEAN app running on port 3000.

From our client side application we are able to get through to backend app using loadbalancer domain name. But we also have a chat feature with socket.io

For socket connection we were not able to use loadbalancer domain name. It throws 400 error.

If we try to use the backend IP directly for socket connection, it works but the if the client is on HTTPS, it creates another problem because backend is http.

Google documentation says loadbalancer supports websockets by default. So not sure what is going on. All other examples I see are relatively old and not relevant I think. Any help is appreciated. Thanks.

Best Answer

For WebSockets over an HTTP(S) load balancer, the Backend Service (response) timeout is a connection lifetime limit (WebSockets connections are killed after the configured response timeout). Hence, the timeout should be set to the maximum amount of time a WebSocket connection will remain open. The appropriate response timeout value is dependent on the application you use.

You will need to do some experiments to find an appropriate response timeout to avoid the connection timeout closures (increase its value slightly and retry; for example, if 30 seconds is insufficient, try 40sec,50 sec etc.).

Found on StackOverflow.