Issue
I am having issues with running websockets on DigitalOcean's App platform. I think I am fundamentally missing a configuration as it relates to Daphne.
Here is my setup:
Daphne 4.0.0 Channels-Redis 3.4.1
Settings.py
INSTALLED_APPS = [
...
# Third-party
'channels',
...
WSGI_APPLICATION = "config.wsgi.application"
ASGI_APPLICATION = "config.asgi.application"
CSRF_TRUSTED_ORIGINS = [
'https://blah.com',
'https://*.blah.com',
]
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
ASGI.py
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.dev')
import django
django.setup()
from django.core.asgi import get_asgi_application
asgi_app = get_asgi_application()
import api_waitlist.routing
from channels.routing import ProtocolTypeRouter, URLRouter, get_default_application
from channels.security.websocket import AllowedHostsOriginValidator
from channels.auth import AuthMiddlewareStack
application = ProtocolTypeRouter({
'http': asgi_app,
'websocket':
AllowedHostsOriginValidator(AuthMiddlewareStack(
URLRouter(
api_waitlist.routing.websocket_urlpatterns
))
), # new
})
DigitalOcean Configurations
DigitalOcean App: "run command" located under App>Settings>Run Command:
daphne -b 0.0.0.0 -p 8080 config.asgi:application
Redis Configuration: Redis is set up using DigitalOcean's managed Redis database. The connection string is provided by DigitalOcean. The app level variable "REDIS_URL" that contains the connection string is:
rediss://default:password_here@fs-dev-redis-do-user-xxxx-0.b.db.ondigitalocean.com:25061
React Frontend (React is within the Django project scaffolding): The React frontend code to establish the websocket connection is:
wb = new WebSocket("ws://127.0.0.1:8000/ws/waitlist/");
The frontend browser console error:
Solution
I was able to resolve this issue specifically for the DigitalOcean app platform:
- The websocket URL must be using "wss" when the site is using https
- The domain is required instead of referencing the localhost
- The port number is not required in the URL
This is what the final URL looks like:
wss://your_domain.com/ws/waitlist/
Answered By - cbirch
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.