Channels: Django Channels in Production Mode

Created on 21 Jul 2020  路  5Comments  路  Source: django/channels

Hello, I'm trying to integrate a chat in my app with Django Channels, which works in development mode, but doesn't work in production mode.
This is some error I get: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.
I've done the steps presented in the Django Channels documentation(https://channels.readthedocs.io/en/latest/deploying.html), but it doesn't work for me.
When I try to run the daphne start server command 'daphne -p 6379 muypicky.asgi:application ', the console freezes and it does nothing.

Daphne command:

daphne -p 6379 muypicky.asgi:application                                                                                                               
2020-07-21 23:06:43,123 INFO     Starting server at tcp:port=6379:interface=127.0.0.1
2020-07-21 23:06:43,124 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2020-07-21 23:06:43,124 INFO     Configuring endpoint tcp:port=6379:interface=127.0.0.1
2020-07-21 23:06:43,125 INFO     Listening on TCP address 127.0.0.1:6379

I'm not sure where I'm mistaken, any help would be appreciated.

myproject/settings.py

ASGI_APPLICATION = 'muypicky.routing.application'

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [('hradutiu.pythonanywhere.com', 6379)],
        },
    },
}

pip freeze

aioredis==1.3.1
asgiref==3.2.10
async-timeout==3.0.1
attrs==19.3.0
autobahn==20.7.1
Automat==20.2.0
cffi==1.14.0
channels==2.4.0
channels-redis==3.0.1
constantly==15.1.0
cryptography==3.0
daphne==2.5.0
Django==3.0.7
hiredis==1.1.0
hyperlink==19.0.0
idna==2.10
incremental==17.5.0
msgpack==1.0.0
Pillow==7.1.2
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.20
PyHamcrest==2.0.2
pyOpenSSL==19.1.0
pytz==2020.1
redis==3.5.3
service-identity==18.1.0
six==1.15.0
sqlparse==0.3.1
Twisted==20.3.0
txaio==20.4.1
zope.interface==5.1.0

Most helpful comment

@Radutiu-Horatiu .
I think pythonanywhere don't have support for Redis-Server https://www.pythonanywhere.com/forums/topic/12039/ .
I have tried most of the cloud services and at last, landed up with a virtual machine to deeply Django channels in production.

Even I tried Redis with other services like an azure Redis catch but it won't allow running channels in port 6379 in https as this is insecure port.
https://stackoverflow.com/questions/62861435/django-channels-and-azure

All 5 comments

This isn't something I've seen before I'm afraid.

Googling the error leads here: https://github.com/facebook/create-react-app/pull/8079#issuecomment-562373869 Suggests you may need to use the wss protocol.

From the console log :

HTTP/2 support not enabled (install the http2 and tls Twisted extras)

This is strange. Daphne requires the tls extras, so I'm not sure if those are installed or not...

https://github.com/django/daphne/blob/9838a173d7ef4204f626f55b0819671473504470/setup.py#L25

They should be.

Please do update if you can make progress.

Hi, it's me again. I changed the way I am initalizing my websocket in JavaScript. I don't get the wss:// error anymore, because I changed it to this:

var loc = window.location;
var wsStart = 'ws://';
if (loc.protocol == 'https:') {
     wsStart = 'wss://'
}
var endpoint = wsStart + 'hradutiu.pythonanywhere.com:6379' + '/ws/chat/' + roomName + '/';
var chatSocket = new WebSocket(endpoint);

But now I get another error:

WebSocket connection to 'wss://hradutiu.pythonanywhere.com:6379/ws/chat/testhradutiu/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

This is my daphne command now:

daphne -p 6379 muypicky.asgi:application
2020-07-22 13:42:55,782 INFO     Starting server at tcp:port=6379:interface=127.0.0.1
2020-07-22 13:42:55,783 INFO     HTTP/2 support enabled
2020-07-22 13:42:55,783 INFO     Configuring endpoint tcp:port=6379:interface=127.0.0.1
2020-07-22 13:42:55,784 INFO     Listening on TCP address 127.0.0.1:6379

settings.py

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [(os.environ.get('REDIS_HOST', 'localhost'),6379)],
        },
    },
}

Any ideas?

Note: I'm hosting this on pythonanywhere.com, could it be a problem with the ip address or port to daphne server?
What should I put instead of 'REDIS_HOST'?
Shouldn't the daphne server start on my domain instead of 127.0.0.1?

@Radutiu-Horatiu .
I think pythonanywhere don't have support for Redis-Server https://www.pythonanywhere.com/forums/topic/12039/ .
I have tried most of the cloud services and at last, landed up with a virtual machine to deeply Django channels in production.

Even I tried Redis with other services like an azure Redis catch but it won't allow running channels in port 6379 in https as this is insecure port.
https://stackoverflow.com/questions/62861435/django-channels-and-azure

@saibhaskar24
Yes, you are right. Thank you.
Do you know any alternative hosting sites where I can deploy my django application and use the django channels there?

@Radutiu-Horatiu
I think heroku has support for this as they have some plugin for redis but I didn't try...
According to me going ahead with virtual machine is the best option.

Was this page helpful?
0 / 5 - 0 ratings