Hello, it’s me again (sorry) I think I managed to debug the websocket, but I have an error, which makes that I receive nothing.
I post all the details, so I followed a documentation that uses redis-server and daphne for the back
gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
gunicorn.service
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=wahab
Group=www-data
WorkingDirectory=/home/wahab/apporder
ExecStart=/home/wahab/apporder/env/bin/gunicorn \
--access-logfile - \
-k uvicorn.workers.UvicornWorker \
--workers 3 \
--bind unix:/run/gunicorn.sock \
OrderLine1.asgi:application
[Install]
WantedBy=multi-user.target
redis-server
wahab@django411:~/apporder$ sudo netstat -lnp | grep redis
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 82375/redis-server
tcp6 0 0 ::1:6379 :::* LISTEN 82375/redis-server
nginx
server {
listen 80;
server_name ****;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/html;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location /ws/ {
proxy_pass http://127.0.0.1:8001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
}
}
daphne.service
[Unit]
Description=WebSocket Daphne Service
After=network.target
[Service]
Type=simple
User=wahab
WorkingDirectory=/home/wahab/apporder
ExecStart=/home/wahab/apporder/env/bin/python /home/wahab/apporder/env/bin/daphne -b 0.0.0.0 -p 8001 OrderLine1.asgi:application
Restart=on-failure
[Install]
WantedBy=multi-user.target
settings.py
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [('localhost', 6379)],
},
},
}
ALLOWED_HOSTS = ['my_server', 'localhost', '127.0.0.1']
Consumers.py
import logging
class HubriseOrderConsumer(ListModelMixin, GenericAsyncAPIConsumer):
print(HubriseOrder.objects)
def get_queryset(self, **kwargs):
logger = logging.getLogger('django')
logger.info((type(HubriseOrder.objects)))
return HubriseOrder.objects.filter(ascount=self.scope["user"]).exclude(status__in=['En attente', 'Rejeter', 'Compléter', 'new', 'Livraison échouer']).order_by('created_at')
serializer_class = HubriseOrderSerializer
permissions = (permissions.AllowAny,)
async def connect(self, **kwargs):
await self.model_change.subscribe()
await super().connect()
@model_observer(HubriseOrder)
async def model_change(self, message, observer=None, **kwargs):
await self.send_json(message)
@model_change.serializer
def model_serialize(self, instance, action, **kwargs):
return dict(data=HubriseOrderSerializer(instance=instance).data, action=action.value)
sorry again for the inconvenience