We've been working on some multiplayer capabilities for our classic card game site [Solitaired](https://solitaired.com/), and have been exploring how to do this using Websockets.

Our stack is:

* AWS Elastic Beanstalk
* Application Load Balancers (ALB, not the classic ELBs)
* Node.js & nginx

I Googled a bunch, but the articles I read (from [2017](https://www.purplesquirrels.com.au/2017/09/getting-websockets-working-with-node-socket-io-and-elastic-beanstalk/) and [2019](https://medium.com/@binyamin/node-websockets-with-aws-elastic-beanstalk-elastic-load-balancer-elb-or-application-load-6a693b21415a) respectively) didn't quite work based on Amazon's latest updates.

So, consider this the 2020 edition of how to get Websockets to work on Elastic Beanstalk with Node.js.

Before starting, first make sure:

* That you've set up your sockets library. I used [socket.io](http://socket.io/).
* Make sure your Elastic Beanstalk environment uses an Application Load Balancer (ALB). You cannot switch from a classic load balancer to an ALB. Therefore, you must make a new environment that mirrors your old one except for the ALB.

Then, make a file in the `.ebextensions` directory in your application root. Call it `websockets.config`.

Then paste the contents below to that file.

**This is correct:**

```
files:   "/etc/nginx/conf.d/websockets.conf":     content: |        proxy_set_header Upgrade $http_upgrade;       proxy_set_header Connection "upgrade"; 
```

Commit this file and deploy to Beanstalk. Voila.

Just for reference, here is the pre-2020 version of the file above.

**Do not do it this way anymore:**

```
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 
```