Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Troubleshooting Websockets with EC2 on AWS using Django

I am using Django-Channels to try to get real time features such as chat/messaging, notifications, etc. Right now, I have gotten everything to work fine on my laptop using the settings described in the docs here: http://channels.readthedocs.io/en/latest/. I use a local redis server for testing purposes.

However, when I deploy to my Amazon EC2 Elastic Beanstalk server (using an AWS ElastiCache Redis), the WebSocket functionality fails. I was reading and I think it is due to the fact that Amazon's HTTPS does not support WebSockets, so I need to switch to Secure TCP. I tried doing that with: https://blog.jverkamp.com/2015/07/20/configuring-websockets-behind-an-aws-elb/ and https://medium.com/@Philmod/load-balancing-websockets-on-ec2-1da94584a5e9#.ak2jh5h0q but to no avail.

Does anyone have any success implementing WebSockets with CentOS/Apache and Django on AWS EB? The Django-Channels package is fairly new so I was wondernig if anyone has experienced and/or overcome this hurdle. Thanks in advance

like image 984
EazyC Avatar asked Apr 28 '16 07:04

EazyC


2 Answers

AWS has launched new Application Load Balancer that supports web sockets. Change your ELB to Application Load Balancer and that will fix your issue.

https://aws.amazon.com/blogs/aws/new-aws-application-load-balancer/

like image 191
error2007s Avatar answered Sep 29 '22 13:09

error2007s


As described here it's possible to run Django Channels on Elastic Beanstalk using an Application Load Balancer.

In a simplified form, it's basically:

  1. Create an ALB
  2. Add 2 target groups, one that points to port 80, and one that points to Daphne port, ie 8080.
  3. Create 2 path rules. Let the default route point to target group 1 (port 80), and set the second to use a relative path, ie. /ws/ and point it to target group 2.
  4. Add Daphne and workers to supervisord (or another init system)
  5. DONE! Access Daphne/websockets through the relative url ws://example.com/ws/.
like image 28
ege Avatar answered Sep 29 '22 14:09

ege