Using WebSockets with EC2 Elastic Load Balancer

Posted on December 11, 2016

A few months ago I had to set up a system using Web­Sockets for live an­nounce­ments in the browser using Sock­ We de­cided to use AWS in­fra­struc­ture to host our plat­form. Get­ting Web­Sockets to work prop­erly with the Elastic Load Bal­ancer did not seem to work cor­rectly at the time. We, thus, opted for a cen­tral master node for all out Web­Socket traf­fic. Nat­u­rally, this ap­proach is not scal­able.

How­ever, re­cently Amazon up­dated their load bal­ancer. In­ter­est­ingly, they named Web­Sockets as one of the newly sup­ported fea­tures of the new load bal­ancer. So, I tried to run one of the new load bal­ancers. Nat­u­rally, the Web­Socket sup­port did not work out of the box. After some search­ing, I was able to find that for Web­Sockets to work prop­erly, you have to en­able stick­i­ness for the target group. For our load bal­anc­ing, we did not want to en­able stick­i­ness for the main func­tion­ality of our ap­pli­ca­tion.

It turns out the so­lu­tion to this dilemma is rather sim­ple: create a new target group for the Web­Sock­ets, and en­able stick­i­ness for that group.

  1. Create a new target group with stick­i­ness en­abled. You can en­able stick­i­ness by editing the At­trib­utes of the target group. Create target group with stickiness

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

That is it. Now Web­Sockets should work cor­rectly.

blog comments powered by Disqus