I have using django channels in my application django , i create an instance Redis and i configure redis url , the internal url ,
the socket endpoints match perfectly in developpement but , don’t match on render
There is my configuration ,
CELERY_BROKER_URL = 'redis://red-chsbth64dadfn62j0b20:6379/0' if DEBUG else 'redis://localhost:6379/0'
# CELERY_BROKER_URL = 'rediss://red-chsbth64dadfn62j0b20:blCjy2z9fq69JowclzcIBuccA5FUjOQm@oregon-redis.render.com:6379/0'
# CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers.DatabaseScheduler'
# django channels configuration
# Channels
ASGI_APPLICATION = 'core.asgi.application'
# CHANNEL_LAYERS = {
# 'default': {
# 'BACKEND': 'channels.layers.InMemoryChannelLayer',
# },
# }
CHANNEL_LAYERS = {
'default': {
# 'BACKEND': 'channels_redis.core.RedisChannelLayer',
"BACKEND": "channels_redis.pubsub.RedisPubSubChannelLayer",
'CONFIG': {
# "hosts": [('127.0.0.1', 6379)],
"hosts": [CELERY_BROKER_URL],
},
},
}
in my poject deployement settings i set the StartCommand
gunicorn core.wsgi:application; daphne -b 0.0.0.0 core.asgi:application
This is my routing.py
from django.urls import path
from .consumers import PositionConsumer,EventConsumer
websocket_urlpatterns = [
path('ws/positions/', PositionConsumer.as_asgi()),
path('ws/events/', EventConsumer.as_asgi()),
]
My Asgi file
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from traccar import routing
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': AuthMiddlewareStack(
URLRouter(
routing.websocket_urlpatterns
)
),
})
There is also my implementation in the template
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
var counter = 0;
var eventSocket = new WebSocket(`${protocol}//${window.location.host}/ws/events/`);
var notification_badge = document.getElementById("notification-badge");
eventSocket.onopen = function (event) {
console.log("connected")
};
// Receive messages from the server
eventSocket.onmessage = function (event) {
console.log(JSON.parse(event.data));
var data = JSON.parse(event.data);
;
fetchAlerts();
}