'Socket.io connecting/disconnecting unexpectedly in production - MEAN stack hosted on Elastic Beanstalk

I have a MEAN stack application hosted on AWS Elastic Beanstalk that uses socket.io This socket.io is connecting/disconnecting unexpectedly.

The socket causes a code of 4xx to the server. After a few 4xx are sent to AWS, the server degrades automatically and the socket disconnects. So it becomes like a loop, AWS first receives the 4xx, then the environment becomes unhealthy, then the socket behaves even more strange because the server is about to go down, etc.

What matters is that the starting point is the 4xx caused by the socket.

The log I have says:
"GET /socket.io/?EIO=3&transport=polling&t=Nx_u0FB HTTP/1.1" 400 62

I tried to add the CORS option to the socket with the app domain as the origin, but it didn't help. Please note that this happens in production only and not on localhost.

Also, please note that this case doesn't happen unless we have like 10-20 users/sockets connecting from different parts of the world. If we have a few sockets, it rarely happens, and sometimes even if we have many users connecting from the same country, it doesn't happen. The behavior is very random.

Anyone can help with this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source