ws://localhost:8000/ws/chat/${roomId}/?token=${accessToken}
wss://api.ethiofirstaid.com/ws/chat/${roomId}/?token=${accessToken}
Trying to do a socket like chat thing python django and it works on my computer but not on the domain name server, what could cause such issues
The "Not Found" error you're seeing when trying to connect to your WebSocket endpoint on the production server is a common issue when deploying Django WebSocket applications. Here are the most likely causes and solutions:
Your production server might not be configured to handle WebSocket connections properly.
For Apache:
mod_proxy_wstunnel is enabledProxyPass /ws/ ws://localhost:8000/ws/
ProxyPassReverse /ws/ ws://localhost:8000/ws/For Nginx:
location /ws/ {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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-Proto $scheme;
}Make sure your ASGI server (like Daphne, Uvicorn, or Hypercorn) is running on the production server, not just the WSGI server.
# Example with Daphne
daphne -b 0.0.0.0 -p 8000 your_project.asgi:application
# Example with Uvicorn
uvicorn your_project.asgi:application --host 0.0.0.0 --port 8000Verify your Django Channels setup in settings.py:
# settings.py
INSTALLED_APPS = [
# ... other apps
'channels',
]
ASGI_APPLICATION = 'your_project.asgi.application'
# Channel layers configuration
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}Check your WebSocket URL routing in asgi.py:
# asgi.py
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
import your_app.routing
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
your_app.routing.websocket_urlpatterns
)
),
})And in your app's routing.py:
# your_app/routing.py
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'ws/chat/(?P<room_id>\w+)/$', consumers.ChatConsumer.as_asgi()),
]Since you're using wss:// (secure WebSocket), ensure:
ws:// to test if SSL is the issueIf you're using a load balancer, it needs to support WebSocket connections:
Create a simple test endpoint to verify your WebSocket setup:
# consumers.py
import json
from channels.generic.websocket import WebsocketConsumer
class TestConsumer(WebsocketConsumer):
def connect(self):
self.accept()
self.send(text_data=json.dumps({
'message': 'Connected successfully!'
}))
def disconnect(self, close_code):
pass
def receive(self, text_data):
self.send(text_data=text_data)The most common cause is usually the server configuration not properly handling WebSocket upgrade requests. Start by checking your web server (Nginx/Apache) configuration and ensure your ASGI server is running alongside your WSGI server.