I have setup my Django server to run on https mode and the API calls are also working. I have also configured Daphne which is starting without any problems. However, when I try to open a secure websocket connection using wss, it fails.
It would be great if I can get any help on this. Thanks.
docker-compose.yml
services:
api:
platform: linux/amd64
container_name: api
build:
context: .
dockerfile: Dockerfile.dev
command: 'sh -c "./manage.py migrate && python manage.py runsslserver 0.0.0.0:8080"'
restart: always
networks:
- default
volumes:
- ./:/app
- $HOME/.aws:/root/.aws:ro
- /var/run/docker.sock:/var/run/docker.sock
- type: bind
source: ./docker/cpuinfo_with_fake_speed.txt
target: /proc/cpuinfo
ports:
- 8080:8080
env_file:
- ./.env
depends_on:
- db
daphne:
platform: linux/amd64
container_name: daphne
build:
context: .
dockerfile: Dockerfile.dev
command: 'sh -c "daphne --ws-protocol [ws, wss] -v 1 -e ssl:8000:privateKey=key.pem:certKey=cert.pem apps.asgi:application"'
restart: always
working_dir: /app
networks:
- default
volumes:
- ./:/app
- /var/run/docker.sock:/var/run/docker.sock
ports:
- 8000:8000
asgi.py
django_asgi_app = get_asgi_application()
from channels.routing import ProtocolTypeRouter, URLRouter
application = ProtocolTypeRouter(
{
"http": django_asgi_app,
"websocket": AuthMiddlewareStack(URLRouter([path('ws/notifications/', NotificationConsumer.as_asgi())])),
}
)
I can see that Daphne service is starting up fine
daphne | 2023-04-28 05:39:32,845 INFO Starting server at ssl:8000:privateKey=key.pem:certKey=cert.pem
daphne | 2023-04-28 05:39:32,847 INFO HTTP/2 support enabled
daphne | 2023-04-28 05:39:32,847 INFO Configuring endpoint ssl:8000:privateKey=key.pem:certKey=cert.pem
daphne | 2023-04-28 05:39:32,891 INFO Listening on TCP address 0.0.0.0:8000
And this is how, I'm trying to open the websocket connection, which fails.
wss://localhost:8000/ws/notifications/?userid=x48