Websockets, Elastic Beanstalk, and Application Load Balancers - 2020 edition

By Neal Taparia - Last updated: 09/27/2023

We've been working on some multiplayer capabilities for our classic card game site Solitaired, 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 and 2019 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.
  • 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 

About the author

Neal Taparia is one of the founders of Solitaired. He loves playing card games and is interested in understanding how games can help with brain training and skills building. In addition to card games, he also likes fishing and mountain biking.
×

Sign in to Solitaired.com with Facebook

Sign in to appear on the leaderboard and save your stats!