Prateek Garg
Prateek Garg

Reputation: 93

HTTP 400 Error on AWS ElasticBeanstalk with ALB and Socket.IO

I have an Elastic Beanstalk environment with Application Load Balancer (ALB) setup on Node.js instance which server both REST APIs as well as Socket.IO connections.

When the number of EC2 instances is more than one some of Socket.IO connections are failing with HTTP 400. The same issue is resolved when there is only one instance.

I have also tried enabling sticky sessions in the ALB and have also created a Redis adapter which connects to my Redis instance in ElasticCache.

Unable to figure out what is causing those HTTP 400 errors.

Socket.IO documentation https://socket.io/docs/v2/using-multiple-nodes/

Upvotes: 0

Views: 1260

Answers (2)

Gianluca Grossi
Gianluca Grossi

Reputation: 41

After two days of research, I found the solution. Use transport "websocket" instead of "polling" (default).

Server:

const io = require('socket.io')(http, {
   cors: {
      origin: '*'
   },
   transports: ['websocket']
})

Client:

import io from 'socket.io-client'
socket = io('wss://your_url/', {
   transports: ['websocket']
})

Upvotes: 4

Prateek Garg
Prateek Garg

Reputation: 93

Eventually had to use Network Load Balancer (NLB) which resolved this issue.

Upvotes: 0

Related Questions