1 year ago

This is something that took me nearly a year to get working. Not full time obviously, but every now and then I would make another attempt at getting it working after the trauma of the previous attempt had faded. None of the examples that people had provided worked for me, and some were out dated and no longer applied. And of course the AWS documentation provided no help whatsoever.

I decided to share my configuration in the hope that it will help someone else. Here is a list of the technologies I am using:

The app is a NodeJS app running on 6.11.X, soon to be 8.4.X

Using the Express server framework and Socket.io for WebSockets

Using AWS Elastic Beanstalk

Nginx with reverse proxy to NodeJS

Using the newer Application Load Balancer

HTTPS is enabled

Front and back end running on same domain

On the server

The node app on the server runs Express, and I have condensed the critical lines into the following to show the Socket.io setup:

Elastic Beanstalk config

Up until now this should all be standard Socket.io configuration. Note that there is no messing around with port numbers, it should work on whatever port/domain your app is using.

The tricky part is getting the WebSockets to work with the load balancer, and the Nginx reverse proxy. Make sure you use the Application Load Balancer, not the ‘Classic’ load balancer. The Application load balancer natively supports WebSockets (and HTTP2), however AWS is not very clear on how to actually make them work.

After probably hundreds of Google searches and reading the the same articles over and over again nothing seemed to work. I eventually stumbled upon an AWS blog post – How to Build a Chat Application with Amazon ElastiCache for Redis. At the very bottom of the post there is one small piece of gold which finally after nearly a year got WebSockets working. The mysterious ‘working’ .ebextensions config!