1
votes

I am running a socket.io node.js app on AWS Elastic Beanstalk. I have not included express.js.

Obviously Nginx proxy on Elastic Beanstalk doesn't pass TCP connections through without manual configuration.

I am trying to connect to it via my Android client or browser.

I have tried literally every solution on the internet, but I either get 502 Bad gateway, or timeout errors.

Should I change the AWS Elastic Beanstalk load balancer to accept TCP not HTTP ?

My node.js app is listening on port 3000 (hardcoded). Works great on localhost.

var server = require('http').createServer(handleRequest);
var io = require('socket.io')(server);

function handleRequest(request, response)
{
    response.end('It Works!! Path Hit: ' + request.url);
}

io.on('connection', function (socket)
{
    console.log('a user connected');

    socket.on('disconnect', function ()
    {
        console.log('user disconnected');
    });

    socket.on('chat message', function (msg)
    {
        console.log('message received on server: ' + msg);

        io.emit('chat message', msg);
    });
});

server.listen(3000, function ()
{
    console.log("Server listening on port:", 3000); //Callback triggered when server is successfully listening. Hurray!
});

Here is my package.json

{
  "name": "qurba-messaging-backend",
  "version": "0.0.1",
  "scripts": {
    "start": "node main.js"
  },
  "dependencies": {
    "socket.io": "^1.4.6"
  }
}

Here is my nginx configurations https://gist.github.com/RobinBuschmann/c42a413ecedee463e5bd8e613ddd8d04

files:
  "/etc/nginx/conf.d/01_websockets.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      upstream nodejs {
        ip_hash;
        server 127.0.0.1:3000;
        keepalive 256;
      }

      server {
        listen 80;

        location / {
          proxy_pass  http://nodejs;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
          proxy_http_version 1.1;
          proxy_set_header  Host  $host;
          proxy_set_header  X-Real-IP $remote_addr;
          proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        }
      }
  "/opt/elasticbeanstalk/hooks/appdeploy/enact/41_remove_eb_nginx_confg.sh":
    mode: "000755"
    owner: root
    group: root
    content : |
      mv /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf.old
1

1 Answers

1
votes

Ended up having to use Express.js using express generator, then used the answer here 502 Bad Gateway Deploying Express Generator Template on Elastic Beanstalk