3

We have and elastic beanstalk instance, we have some rest services and socket.io too. express is being started ad port 80. I started socket.io at 3001. but unable to connect this socket. I have also switched off the proxy, from nginx to off. code for socket is as below

var express         = require('express'),
    app             = express(),
    server          = require('http').createServer(app),
    io              = require('socket.io')(server), //make sockets
    port            = 3001;//port for socket

//initiate server
server.listen(port, function () {
    console.log('Socket.io listening at port %d', port);
});
Husnain Ashfaq
  • 142
  • 1
  • 7

5 Answers5

10

By default an elastic beanstalk instance has an nginx proxy in front of it that is not configured to allow webSockets. You can either turn the proxy off or you can configure the nginx instance to support webSockets.

Here's an article that describes how to enable support for webSockets in the proxy.

And, here are two articles that describe disabling the proxy entirely here and here.

And, even a StackOverflow question/answer on the same topic:

Websockets with socket.io on AWS Elastic Beanstalk

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

I also ran into this problem. Now working with:

  • On Elastic Beanstalk: Nginx turned off (though the above solution should work, just wanted to take it step by step).

  • On Elastic Beanstalk: Load balancer as here: https://i.ibb.co/Lp7P9fT/Screen-Shot-2019-10-07-at-2-48-02-PM.png (can't post yet, reputation too low)

  • In Node/Express: const io = require('socket.io')(3030)
  • On Client: let connection = io(https://www.myurl.com:3030)
Niko Dunk
  • 448
  • 6
  • 11
0

I can not say for sure but I think you don't have port 3001 open on your security groups. You must allow traffic on port 3001 for ec2 sg of your eb app so that ELB of your elb app has access. Also, make sure you configure SG of ELB so that anyone from internet can access the socket.io port on the EC2 instances via ELB. However, you will need to use TCP based listener with proxy protocol enabled for socket.io to work well. I have not checked this but I don't think you can configure port 3001 on listener section. In such case, you can use nginx and configure upstream and location blocks so that you send requests to correct upstream. You can have a look on .ebextensions based configurations so that you can replace nginx configurations during deploy appropriately. The downside of this is that your script might need change along with the upgrades of elastic beanstalk solution stack.

Samar
  • 1,865
  • 12
  • 13
0

Create a new config file in .ebextensions.

Add this:

container_commands:
 enable_websockets:
  command: |
   sed -i '/\s*proxy_set_header\s*Connection/c \
          proxy_set_header Upgrade $http_upgrade;\
          proxy_set_header Connection "upgrade";\
  ' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf
Lorien
  • 442
  • 4
  • 6
-11

It was issue with security restrictions. otherwise code works fine. thanks for help guys.

Husnain Ashfaq
  • 142
  • 1
  • 7