Node Websockets with AWS Elastic Beanstalk — Elastic Load Balancer (ELB) or Application Load Balancer (ALB)
Looking for a great (remote only) React Dev? Visit my profile on LinkedIn and say hi! 😃
It took me an entire day to figure this out. The answer popped into my head while I was playing Rummikub with my Mum and Dad (they beat me). So I felt stupid for two reasons… 🙁
Terminology: Elastic Load Balancer — ELB. Application Load Balancer — ALB.
Here’s the part that it took me 13 hrs to piece together: ELB supports TCP (as well as HTTP, HTTPS and SSL), whereas ALB ONLY supports HTTP and HTTPS.
That means that if you want to use ALB, you can’t use a true Websocket library like ws
(https://www.npmjs.com/package/ws) because ws
runs over TCP.
So in order to use Websockets over ALB, you have to use a library like socket.io
(https://www.npmjs.com/package/socket.io) which starts by using Long Polling and then tries to upgrade the connection to Websockets. This from their README:
Connections are established even in the presence of:* proxies and load balancers.
* personal firewall and antivirus software.For this purpose, it relies on Engine.IO, which first establishes a long-polling connection, then tries to upgrade to better transports that are "tested" on the side, like WebSocket.