Using WebSockets with EC2 Elastic Load Balancer

Posted on December 11, 2016

A few months ago I had to set up a system using WebSockets for live announcements in the browser using We decided to use AWS infrastructure to host our platform. Getting WebSockets to work properly with the Elastic Load Balancer did not seem to work correctly at the time. We, thus, opted for a central master node for all out WebSocket traffic. Naturally, this approach is not scalable.

However, recently Amazon updated their load balancer. Interestingly, they named WebSockets as one of the newly supported features of the new load balancer. So, I tried to run one of the new load balancers. Naturally, the WebSocket support did not work out of the box. After some searching, I was able to find that for WebSockets to work properly, you have to enable stickiness for the target group. For our load balancing, we did not want to enable stickiness for the main functionality of our application.

It turns out the solution to this dilemma is rather simple: create a new target group for the WebSockets, and enable stickiness for that group.

  1. Create a new target group with stickiness enabled. You can enable stickiness by editing the Attributes of the target group. Create target group with stickiness

  2. In the settings of the ELB, add a new rule for http(s) Add rule for /

That is it. Now WebSockets should work correctly.

blog comments powered by Disqus