skip to Main Content

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/

2

Answers


  1. Chosen as BEST ANSWER

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


  2. 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']
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search