Answer 1:
The problem was in my nginx file it is mandatory to use some proxy headers for websockets.
here is my updated nginx file code snippet
location /ws/ {
proxy_pass http://app-development-asgi/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
But now I get 500 error while testing on postman: Container log error:
IP:36032 - - [XX:XX:XX] "WSCONNECTING /chat/1/" - - ERROR Exception inside application: No route found for path 'chat/1/'. My requirements.txt
django-cacheops==7.0.1
django-cors-headers==3.14.0
django-debug-toolbar==4.0.0
django-environ==0.4.5
django-mysql==3.9.0
django-ninja==0.22.2
django-redis==5.3.0
django-simple-history==2.12.0
django-storages==1.10
Django==5.0.4
djangorestframework_simplejwt==5.3.1
djangorestframework==3.14.0
psycopg2-binary==2.9.9
pillow==10.4.0
channels==4.1.0
channels_redis==4.2.0
daphne
drf-yasg==1.21.8
firebase-admin==6.6.0
pytz
gunicorn==23.0.0
Answer 2:
The second problem was because of nginx which was treating /ws/ differently, when I tried ws/ws/ 2 times it worked. Now for the final solution I have removed ws/ in asgi.py so I don't have to make any changes in my frontend.
my final asgi.py
application = ProtocolTypeRouter({
"http": application,
"websocket": JWTAuthMiddleware(
URLRouter([
path('chat/<str:room_name>/', ClubFeedConsumer.as_asgi()),
path('location/<str:ride_id>/', RideLocationConsumer.as_asgi()),
])
),
})
Hope this helps if anyone is facing the same problem even after years. Have a nice day :)