Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use socket.io through google load balancing

We are having some problems when trying to connect socket.io to a node.js compute engine instance through google load balancing.

If I connect from my browser directly to the external IP of my node.js everything works fine.

If I try to connect to this same node.js through the load balancing (which will be the production architecture), the socket keeps disconnecting all the time.

We tried to configure Load Balancing with sessionAffinity without success.

Any tips? Thanks

like image 625
JLCDev Avatar asked Dec 20 '17 03:12

JLCDev


People also ask

How does Google do load balancing?

Google's global load balancer knows where the clients are located and directs packets to the closest web service, providing low latency to users while using a single virtual IP (VIP). Using a single VIP means we can increase the time to live (TTL) of our DNS records, which further reduces latency.

How does Socket.IO work internally?

Socket.IO allows bi-directional communication between client and server. Bi-directional communications are enabled when a client has Socket.IO in the browser, and a server has also integrated the Socket.IO package. While data can be sent in a number of forms, JSON is the simplest.


1 Answers

By Default The timeout set for http load balancing is 30 seconds by default (Source), this applies to web sockets which in turn is used by socket.io when the protocol is supported by the back-end. Therefore you need to do the following to increase the time out (Source).

To configure the timeout setting:

  • GO TO THE LOAD BALANCING PAGE
  • Select the Edit pencil for your load balancer.
  • Select Backend configuration.
  • Select the Edit pencil for the Backend service.
  • On the line for Protocol, Port, and Timeout settings, select the Edit pencil.
  • Enter a new Timeout Setting in seconds.
  • Click the Update button for the load balancer.
like image 145
Kalana Demel Avatar answered Sep 30 '22 01:09

Kalana Demel