Websocket with django, docker-compose, nginx, daphne.
Regular http connections are working fine, but there's no output coming from websocket connections. The websocket doesn't appear to be active.
Connecting through javascript produces an error:
new WebSocket('ws://127.0.0.1:8000/ws/pollData');
// WebSocket connection to 'ws://127.0.0.1:8000/ws/pollData' failed:
or when connecting to ws://localhost/ws/pollData:
new WebSocket('ws://localhost/ws/pollData');
// Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
docker-compose.yaml
version: '3'
services:
volume_configurer:
image: busybox
volumes:
- shared:/shared:z
- static:/static:z
command: ["/bin/sh", "-c", "
mkdir -p /static;
chmod -R 777 /static;
mkdir -p /shared/sync;
chmod -R 777 /shared/sync;
echo STARTED > /shared/sync/volumesetter && chmod a+r /shared/sync/volumesetter"]
db:
container_name: postgresdb
image: postgres:latest
restart: always
env_file:
- project.env
ports:
- 5432:5432
volumes:
- postgres-data1:/var/lib/postgresql/data1:z
web:
build:
context: ./
dockerfile: ./mdb/Dockerfile
container_name: django
command: >
daphne mdb.asgi:application -b 0.0.0.0 -p 8000
env_file:
- project.env
expose:
- 8000
depends_on:
- db
volumes:
- ./mdb:/home/app/web/:z
- static:/home/app/web/static/:z
- shared:/uploads/:z
nginx:
container_name: nginx
image: nginx
restart: always
ports:
- 80:80
volumes:
- ./nginx:/etc/nginx/conf.d:z
- static:/home/app/web/static/:z
depends_on:
- web
- db
volumes:
postgres-data1:
static:
shared:
nginx.conf:
upstream mdb {
server django:8000;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name localhost;
client_max_body_size 2000M;
location /static/ {
alias /home/app/web/static/;
}
location /ws/ {
proxy_pass http://mdb;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
location / {
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_pass http://mdb;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
consumer.py
from channels.generic.websocket import WebsocketConsumer
import json
class DashConsumer(WebsocketConsumer):
def connect(self):
print('==================2')
self.accept()
def disconnect(self, close_code):
print('==================1')
pass
def receive(self, text_data):
print('==================0')
text_data_json = json.loads(text_data)
message = text_data_json['message']
self.send(text_data = json.dumps({
'message': message
})
)
From a python view, connecting to a websocket does not produce any output when connecting to ws://localhost:8000/ws/pollData. However, it also doesn't crash. Whereas when connecting to something else, say, ws://localhost/ws/pollData, it crashes with ConnectionRefusedError: [Errno 111] Connection refused. However, there's no output from django when using ws.connect(), i.e., print('==================<->') in consumer.py is not firing.
views.py
ws = websocket.WebSocket()
# does not print
print(ws.connect('ws://localhost:8000/ws/pollData'))
# also does not print
print(ws.send('{"message": "test from django"}'))
# prints "still here"
print('still here')
# this crashes
print(ws.connect('ws://localhost:8000/ws/pollData'))
# does not print
print('still here')
asgi.py
import os
from django.conf.urls import url
from django.core.asgi import get_asgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mdb.settings")
django_asgi_app = get_asgi_application()
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from chat.consumer import DashConsumer
application = ProtocolTypeRouter({
"http": django_asgi_app,
"websocket": AuthMiddlewareStack(
URLRouter([
url(r"^ws/pollData$", DashConsumer.as_asgi()),
])
),
})
settings.py
WSGI_APPLICATION = 'mdb.wsgi.application'
ASGI_APPLICATION = 'mdb.routing.application'
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels.layers.InMemoryChannelLayer"
},
}
Help is much appreciated!
Edit:
It appears that javascript is connecting when using the address of either ws://localhost/ws/pollData or ws://0.0.0.0/ws/pollData as in either case a connect Event occurs and the network panel shows an open websocket connection. However, in python, both of these address cause ConnectionRefusedError: [Errno 111] Connection refused, i.e.:
ws = websocket.WebSocket()
# appears to connect but does not send anything to javascript
ws.connect('ws://localhost:8000/ws/pollData')
ws.send('{"message": "test from django"}')
# crashes
ws.connect('ws://localhost/ws/pollData')
# also crashes
ws.connect('ws://0.0.0.0/ws/pollData')