Node Websockets with AWS Elastic Beanstalk — Elastic Load Balancer (ELB) or Application Load Balancer (ALB)

Ben Grunfeld
4 min readFeb 6, 2019

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.

--

--

Ben Grunfeld
Ben Grunfeld

Written by Ben Grunfeld

I’m a Front End Engineer who loves React, NextJS, and GraphQL. Looking for a developer in #Israel? Contact me at: https://www.linkedin.com/in/bengrunfeld/

Responses (8)