7 months ago
Hello Railway Support,
I’m currently running a WebSocket server using Django Channels and Daphne to serve my ASGI application, while using Docker as my builder. However, I keep encountering a 404 error whenever I attempt to connect via the WebSocket URL (wss://
) & (ws://
) . It connects on my local host, but fails to connect resulting in a 404 error on my Railway server.
I’ve tried both including and excluding the port in the WebSocket URL, but the issue persists. My setup does not include a frontend domain—only a backend domain.
Am I supposed use a Caddy Proxy Server to handle WebSockets in this environment? I've tried using Caddy Proxy Server with just the Backend Domain and Port (Dummy data was given to Frontend ENVs), but no response in my Backend Server, and I just get a 502 error on the Caddy Proxy Server. Any guidance would be greatly appreciated.
Thank you for your assistance!
Best regards,
Anthony Humphreys
.
.
.
.
.
.
.
.
.
.
```
Starting server at tcp:port=8001:interface=0.0.0.0
HTTP/2 support not enabled (install the http2 and tls Twisted extras)
Configuring endpoint tcp:port=8001:interface=0.0.0.0
HTTPFactory starting on 8001
Listening on TCP address 0.0.0.0:8001
```
"GET /ws/notifications/ HTTP/1.1" 404 179 "-" "-"
````
.
.
.
.
.
.
.
Supervisord.conf
[program:daphne]
command=daphne Backend.asgi:application --port 8001 --bind 0.0.0.0 -v2
directory=/app
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0
I've tried Uvicorn and that gave me a 404 error also.
.
.
.
.
.
.
.
ALLOWED_HOSTS = ['***-*******-****.up.railway.app', 'localhost', '127.0.0.1', '0.0.0.0']
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
USE_X_FORWARDED_HOST = True
SECURE_SSL_REDIRECT = False
CSRF_TRUSTED_ORIGINS = [
'https://***-*******-****.up.railway.app',
'wss://***-*******-****.up.railway.app', # Allow secure WebSocket connections
]
CORS_ALLOWED_ORIGINS = [
'https://***-*******-****.up.railway.app',
'wss://***-*******-****.up.railway.app', # Allow secure WebSocket connections
]
ASGI_APPLICATION = 'Backend.asgi.application'
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [(os.environ.get('REDIS_BACKEND'))],#Check comment below
},
},
}
INSTALLED_APPS = [
'daphne',
...
'channels',
'home',
]
I put my Caddy Proxy domain, in Allowed but would still get 502.
.
.
.
.
.
.
import os
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
from channels.auth import AuthMiddlewareStack
from home.routing import websocket_urlpatterns
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Backend.settings')
# Initialize HTTP application
django_asgi_app = get_asgi_application()
# Define the application protocol router
application = ProtocolTypeRouter({
"http": django_asgi_app,
"websocket": AuthMiddlewareStack(
URLRouter(
websocket_urlpatterns
)
),
})
I get the WebSocket started but can never connect to it. I also tried CookieMiddleware I believer (I know it wouldn't get passed the 404, but worth a try)
.
.
.
.
.
.
from django.urls import re_path
from .consumers import NotificationConsumer
websocket_urlpatterns = [
re_path(r'ws/notifications/$', NotificationConsumer.as_asgi()), # Using regex for matching
]
I tried different ways, of my URL pattern, like without re_path, still didn't work.
wss://***-*******-****.up.railway.app/ws/notifications/
ws://***-*******-****.up.railway.app/ws/notifications/
wss://***-*******-****.up.railway.app:8001/ws/notifications/
ws://***-*******-****.up.railway.app:8001/ws/notifications/
All of these gave me a 404 still.
8 Replies
7 months ago
This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.
Status changed to Open brody • 7 months ago
7 months ago
I'm using both, guinicorn for my wsgi/https, and daphene for my asgi/wss. Like I can only use daphne, your saying I can't use both right? I am using daphene, but guinicorn is set before daphene, maybe that sets hierarchy priority. Let me test and see.
[program:daphne]
command=daphne Backend.asgi:application --port 8001 --bind 0.0.0.0 -v2
directory=/app
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0
```
Starting server at tcp:port=8001:interface=0.0.0.0
HTTP/2 support not enabled (install the http2 and tls Twisted extras)
Configuring endpoint tcp:port=8001:interface=0.0.0.0
HTTPFactory starting on 8001
Listening on TCP address 0.0.0.0:8001
```
"GET /ws/notifications/ HTTP/1.1" 404 179 "-" "-"
````
7 months ago
Another question, do I need to use a Caddy Proxy Server to use websockets for django channel?
7 months ago
You are massively overcomplicating this, run daphne and only daphne.
No gunicorn, no supervisord, No Caddy, no NGINX, nothing else except daphne.
7 months ago
I did get a 502 error, let me look at that deeper, which is progress none the less! Thank you so much!!! I literally spent all weekend on that issue.
7 months ago
For that, please read this docs page - https://docs.railway.app/guides/fixing-common-errors#application-failed-to-respond
7 months ago
Dude you are literally so awesome, tell your boss I told him to give your trousers a raise haha jk. You have no clue how irritated I've been with this code, I couldn't get off that 404 error for 3 days, and I fixed that 502 error in 1 try. Yo, you ain't Brody no more, you the Syntax Savior.
Status changed to Solved brody • 7 months ago