# Secure Websocket (with HTTPS)

**URL:** <https://forum.djangoproject.com/t/secure-websocket-with-https/16801>\
**Category:** Deployment\
**Created:** [November 3, 2022, 9:29pm UTC](https://forum.djangoproject.com/t/secure-websocket-with-https/16801 "2022-11-03T21:29:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MalloryLP](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mallorylp/32/10670_2.png) [@MalloryLP](https://forum.djangoproject.com/u/MalloryLP)\
**Post date:** [November 3, 2022, 9:29pm UTC](https://forum.djangoproject.com/t/secure-websocket-with-https/16801/1 "2022-11-03T21:29:37Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/8/89ae158475ac9da15fc2dcb99be62d8d94da2754.png)

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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 4, 2022, 12:51am UTC](https://forum.djangoproject.com/t/secure-websocket-with-https/16801/2 "2022-11-04T00:51:32Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![MalloryLP](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mallorylp/32/10670_2.png) [@MalloryLP](https://forum.djangoproject.com/u/MalloryLP)\
**Post date:** [November 4, 2022, 8:11am UTC](https://forum.djangoproject.com/t/secure-websocket-with-https/16801/3 "2022-11-04T08:11:47Z")

</div>

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

Here is my settings.py :

```auto
ASGI_APPLICATION = 'sendapp.asgi.application'

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

```

The versions I use :

```auto
django==4.0.1
channels==3.0.4

```

And the asgi.py part:

```auto
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))
})

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 4, 2022, 1:44pm UTC](https://forum.djangoproject.com/t/secure-websocket-with-https/16801/4 "2022-11-04T13:44:22Z")

</div>

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?)

---

<div class="post-metadata">

**Author:** ![MalloryLP](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mallorylp/32/10670_2.png) [@MalloryLP](https://forum.djangoproject.com/u/MalloryLP)\
**Post date:** [November 13, 2022, 10:30am UTC](https://forum.djangoproject.com/t/secure-websocket-with-https/16801/5 "2022-11-13T10:30:02Z")

</div>

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 :

```auto
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 :

```auto
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 :

```auto
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 :

```auto
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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 13, 2022, 12:35pm UTC](https://forum.djangoproject.com/t/secure-websocket-with-https/16801/6 "2022-11-13T12:35:54Z")

</div>

For `DJANGO_SETTINGS_MODULE`, see [https://docs.djangoproject.com/en/4.1/topics/settings/#designating-the-settings](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](https://github.com/django/daphne)

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.

---

<div class="post-metadata">

**Author:** ![mtb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mtb/32/12628_2.png) [@mtb](https://forum.djangoproject.com/u/mtb)\
**Post date:** [March 10, 2023, 11:01pm UTC](https://forum.djangoproject.com/t/secure-websocket-with-https/16801/7 "2023-03-10T23:01:19Z")

</div>

Hey,  
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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 10, 2023, 11:27pm UTC](https://forum.djangoproject.com/t/secure-websocket-with-https/16801/8 "2023-03-10T23:27:48Z")

</div>

Please open up a new topic for this discussion.

---

<div class="post-metadata">

**Author:** ![mtb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mtb/32/12628_2.png) [@mtb](https://forum.djangoproject.com/u/mtb)\
**Post date:** [March 11, 2023, 12:05am UTC](https://forum.djangoproject.com/t/secure-websocket-with-https/16801/9 "2023-03-11T00:05:45Z")

</div>

> [@Websocket + Django on production (https)](https://forum.djangoproject.com/t/websocket-django-on-production-https/19424):
>
> Dear guys, I am struggling with deployment of my web app that contains real time chat as a feature. The web app works fine on localhost. When it comes to deployment to production + https, the websocket client (browser) couldn’t connect to our consumer. We have set the configuration of ngnix and location /ws/ + proxy things + port 5000… The problem is not with our daphne, becasue we have tested as well with another asgi server uvicorn but same error/warning is showing in server log: WARNING …
