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.

javascript:
image

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.

Thanks,

MLP.

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 settings.py :

ASGI_APPLICATION = 'sendapp.asgi.application'

# LEARN CHANNELS
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer"
    },
}

The versions I use :

django==4.0.1
channels==3.0.4

And the asgi.py 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 :

INSTALLED_APPS = [
    'channels',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'sslserver',
    'accounts',
    'chat',
]

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

# LEARN CHANNELS
CHANNEL_LAYERS = {
    "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 .\manage.py runsslserver --certificate .\sendapp\certif.crt --key .\sendapp\code.key 0.0.0.0:8000

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 manage.py :

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 DJANGO_SETTINGS_MODULE, see https://docs.djangoproject.com/en/4.1/topics/settings/#designating-the-settings

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

Keep in mind that with the way your asgi.py 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.