Django socket on Digitalocean, path not found
INSTALLED_APPS = [
'daphne',
'channels',
]
ASGI_APPLICATION = 'HungerPlace.asgi.application'
CHANNEL_LAYERS = {
'default': {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [REDIS_URL],
},
},
}
consumers.py
class OrderConsumer(AsyncWebsocketConsumer): # Cambiato a AsyncWebsocketConsumer
async def connect(self):
user = self.scope["user"]
struttura_id = await self.get_user_structure_id(user)
if user.is_authenticated and struttura_id:
self.group_name = f"orders_{struttura_id}"
await self.channel_layer.group_add(self.group_name, self.channel_name)
await self.accept()
else:
await self.close()
async def disconnect(self, close_code):
if hasattr(self, "group_name"):
await self.channel_layer.group_discard(self.group_name, self.channel_name)
async def send_order(self, event):
if "data" in event:
# await self.send(event["data"])
await self.send(text_data=json.dumps(event["data"]))
else:
print(f"Received event without 'data': {event}")
async def receive(self, text_data):
# Log per il debug
await self.send(text_data="Message received")
@sync_to_async
def get_user_structure_id(self, user):
"""Esegue operazioni sincrone in un thread separato."""
if user.is_authenticated and user.profilo.strut:
return user.profilo.strut.id
return None
routing.py
from django.urls import path, re_path
from .consumers import OrderConsumer
websocket_urlpatterns = [
path('ws/orders/', OrderConsumer.as_asgi()),
]
asgi.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'HungerPlace.settings')
django_asgi_app = get_asgi_application()
from login.routing import websocket_urlpatterns
application = ProtocolTypeRouter({
'http': django_asgi_app,
'websocket': AuthMiddlewareStack(
URLRouter(
websocket_urlpatterns
)
),
})
the project is using django-allauth, channel, daphne. But the main think is when I am on localhost it does work. when I deploy on digitalocean (it is not a droplet, it is an app), it does not find the url
Not Found: /ws/orders/
I am trying a lot and anythink I can find. I cannot find a solution