9

I am trying to get a chat app powered by django channels to work on AWS Elastic Beanstalk with a load balancer.

I am basically modifying the code from https://github.com/jacobian/channels-example to work with Elastic Beanstalk. I am able to successfully run it locally on with the command

python manage.py runserver

The problem is when I deploy it with Elastic Beanstalk, I get the following error when the chat app is launched

WebSocket connection to 'wss://mydomain.com/test/' failed: Error 
during WebSocket handshake: Unexpected response code: 200

I tried solutions proposed at https://stackoverflow.com/a/29831723/3667089 but it just showed a different error code

WebSocket connection to 'wss://mydomain.com/test/websocket' failed: 
Error during WebSocket handshake: Unexpected response code: 404

I also already changed the load balancer listener port to TCP 80 and obtained a SSL certificate to change the secure listener port to SSL 443 but still get the same error.

I also read Websockets with socket.io on AWS Elastic Beanstalk but there isn't an option to configure the proxy server for Django, I think it is using Apache by default.

What am I missing for the configuration of Elastic Beanstalk to make it work?

Is there any way to change this so we can run daphne server with asgi?

Community
  • 1
  • 1
user3667089
  • 2,996
  • 5
  • 30
  • 56
  • I'm running channels on an AWS VPS, and I had to use supervisor to get it to work. The channels docs say that you need to run both the server (`venv/bin/daphne app.asgi:channel_layer`) and workers (`python manage.py runwoker`) in order to get things working. I can post my supervisord.conf in the answers if you want, but I'm not sure how things work on Elastic Beanstalk. – Brobin Dec 02 '16 at 20:08
  • @Brobin Yes that will be greatly appreciated – user3667089 Dec 02 '16 at 20:11
  • Sorry if i'm too late, but may i know what load balancer you're using? If it's classic, it doesn't support websockets natively. – Abhishek Menon Oct 13 '17 at 18:49

1 Answers1

1

I'm not on Elastic Beanstalk, but here is my configuration for a VPS. Ubuntu 14.04 with nginx and supervisor. Supervisor's job is to make sure that the server and worker process are always running. Nginx listens to port 8000 on localhost and forwards that to 8080 and 443.

# nginx.conf
server {
    listen 8080 default_server;
    server_name example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 default_server ssl;
    server_name example.com;

    # ... SSL stuff

    # Send root to the ASGI server
    location / {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

    # Static Files
    location /static/ {
        root /home/ubuntu/project;
    }

    # Media Files
    location /media/ {
        root /home/ubuntu/project;
    }
}

Here's what my configuration for supervisor looks like. I start the server by simply restarting supervisor sudo service supervisor restart

# supervisord.conf
[program:project_server]
directory=/home/ubuntu/project/
command=/home/ubuntu/project/venv/bin/daphne project.asgi:channel_layer --port 8000 --bind 0.0.0.0

[program:project_worker]
process_name=project_worker%(process_num)s
numprocs=3
directory=/home/ubuntu/project/
command=/home/ubuntu/project/venv/bin/python /home/ubuntu/project/manage.py runworker

[group:project]
programs=project_server,project_worker
Brobin
  • 3,241
  • 2
  • 19
  • 35