Websocket error when using Elastic Beanstalk with Django channels
Asked Answered
M

2

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://mcmap.net/q/400295/-i-get-a-status-200-when-connecting-to-the-websocket-but-it-is-an-error 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?

Magnetism answered 9/9, 2016 at 6:36 Comment(3)
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.Hovercraft
@Hovercraft Yes that will be greatly appreciatedMagnetism
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.Renzo
H
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
Hovercraft answered 2/12, 2016 at 20:20 Comment(0)
P
0

I Think The Your Issue Might be related to only Load Balancer You Had To Create the New Certificate and apply that to your load balancer so that they will allow the communication between your ec2 machine instance and the web socket url.

&

the another way to resolve this problem are you can use your EC2 server IP Address with your like this : " ws://129.12.11.54 " but this is not a efficient way to communicate with websockets of django channels this way might harm your privay and also using this way you cannot the get the secured web sockets connection you willl get " ws:// " and not " wss:// "

i am suffered through same issue if you have efficient solution please convey me tooo....

Please answered 16/9, 2023 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.