Secure Websocket (with HTTPS)

Hello to everyone,

I am working with a Django development server in version 4.0.1 which uses an SSL certificate (https). My application uses websockets and there is the problem: it doesn’t work. Please note that the use of websockets works without SSL certificates.


I’m new and I can’t show you more screenshots on this post…

Do you have an idea? I’ve read that all you have to do is change “ws://” to “wss://” but that doesn’t seem to be enough.



Which development server are you using? Are you using the Daphne runserver or something else?

Which version of channels are you using? (Looking ahead, what are you planning to use as your deployment environment?)

Side note: Please don’t past images of code here. When you wish to share code, copy/paste the text of the code into the body of your post or comment, surrounded by lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted, which is critical with Python. (Yes, you can use this same method also for JavaScript, HTML, or any other text for which you want to maintain formatting.)

I enable channels layer via In-memory channel layer. I’m not using Daphne or Redis.

Here is my :

ASGI_APPLICATION = 'sendapp.asgi.application'

    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer"

The versions I use :


And the part:

from chat.routing import ws_urlpatterns

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sendapp.settings')

application = ProtocolTypeRouter({
  #'https': get_asgi_application(),
  'websocket': AuthMiddlewareStack(URLRouter(ws_urlpatterns))

What server are you running to allow your websocket to connect? (Or, to phrase it another way, what process is listening for a websocket connection?)

Sorry for the late reply. I come back to you with some news : i understand i need to use a daphne server for secure websockets.

I can’t use secure websockets on Django with the sll enabled. I use the sslserver package for Django to allow HTTPS on the development server. My goal is to make a secure chat.

Here is the configuration :


#WSGI_APPLICATION = 'sendapp.wsgi.application'
ASGI_APPLICATION = 'sendapp.asgi.application'

    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer"

Concerning the asgi file :

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sendapp.settings')

application = ProtocolTypeRouter({
  'https': get_asgi_application(),
  'websocket': AuthMiddlewareStack(URLRouter(ws_urlpatterns))

I start the Django server this way :

python .\ runsslserver --certificate .\sendapp\certif.crt --key .\sendapp\code.key

I understand that to use secure websockets, you have to use a Daphne server. So I tried to run it in its basic configuration in the root of :

daphne sendapp.asgi:application

but i have this error code in the shell :

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.


For running Daphne with ssl, see GitHub - django/daphne: Django Channels HTTP/WebSocket server

Keep in mind that with the way your file is configured, Daphne is going to handle both your regular Django requests and your Channels websocket requests. You will not run both runsslserver and daphne at the same time.

Any solution for that? I am struggling with a similar issue for couple of weeks and no clear solution on net. Daphne couldn’t find path for channel path on production through wss. Nginx is configured well, I can see logs on daphne as listener coming from client but it should path /ws/… NOT FOUND 404

Please open up a new topic for this discussion.

1 Like