How to deploy Node.js WebSocket server on Amazon Elastic Beanstalk?

Using the Elastic Beanstalk web console, I launched a new Web Server 1.0 environment with:

  • Predefined Configuration: Node.js, 64bit Amazon Linux 2014.09 v1.0.9
  • Type of medium: load balancing, autoscaling

and set the proxy server equal .

I successfully compressed and downloaded my code through the console:

package.json

{
  "name": "cool",
  "version": "0.0.0",
  "dependencies": {
    "ws": "0.4.x"
  }
}

server.js

var wss = new (require('ws')).Server({port: (process.env.PORT || 3000)})
wss.on('connection', function(ws) {
  console.log('connected')
  ws.on('message', function(message) {
    console.log(message)
    ws.send(message)
  });
  ws.on('close', function() {
    console.log('disconnected')
  })
})

I also tried to include the directory node_modules.

After launch:

wscat -c ws://default-environment-xxxxxxxxxx.elasticbeatalk.com/

I returned:

Error: unexpected server response (200)

How to learn more about this error?

Amazon sets the PORT environment variable to 8080. So I also tried:

wscat -c ws://default-environment-xxxxxxxxxx.elasticbeatalk.com:8080/

But it just hung and then came back:

Error: connect ETIMEDOUT

, WebSocket, , , Heroku WebSocket, 60 .

, ?

+1
2

package.json ( , --save npm install ing), node_modules.

, process.env.PORT - AWS EB 8081.

, console.log(process.env.PORT) , ssh tail -f var/log/nodejs/nodejs.log ( node.js). , ssh-.

, ws.on('error',...) , .

+1

, , - ELB - ( nginx).

, , :

  • .ebextensions ( )
  • , 01_nginx.config
  • : container_commands:

    01_nginx_websockets:
        command: |
            sed -i '/\s*proxy_set_header\s*Connection/c \
            proxy_read_timeout 36000s; \
            proxy_set_header Upgrade $http_upgrade; \
            proxy_set_header Connection "upgrade"; \
            ' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf
    

!

PS: proxy_read_timeout, . , , , .

0

Source: https://habr.com/ru/post/1531921/


All Articles