Hi All,
On the community forums, I’ve seen people having trouble with django channels on here, but no solutions. So I’m hoping I can get some help. I have a django backend that’s using Django Channels, Daphne, and Redis for websockets.
Everything works fine in the local dev environment. But when I deployed onto Render the backend endpoint throws a 403 forbidden error with Postman and my frontend. This is after I created a Redis instance on Render and configured my settings.py file to use the internal url in production. I put in a number of logs to see if the backend is connecting to the redis instance and it seems like it’s working.
But when I try to connect on the frontend the backend logs show the websocket connection is being rejected. For context, I’m using websockets for a dynamic search bar and the following is the failed connection after entering in a search query:
10.204.48.70:50006 - - [22/Apr/2025:15:32:05] “WSCONNECTING /ws/search/Mrs./” - -
10.204.48.70:50006 - - [22/Apr/2025:15:32:05] “WSREJECT /ws/search/Mrs./” - -
10.204.48.70:50006 - - [22/Apr/2025:15:32:05] “WSDISCONNECT /ws/search/Mrs./” - -
I was troubleshooting this with chatgpt, but no luck. Here’s the redis url section of my settings.py file:
Fetch the REDIS_URL environment variable
REDIS_URL = os.getenv(‘REDIS_URL’)
Set default Redis host for local development
if os.getenv(‘DJANGO_DEBUG’, ‘False’) == ‘True’:
# Local development environment, use localhost
redis_host = ‘127.0.0.1’
redis_port = 6379
else:
# Production environment, use the Redis URL from the environment
if REDIS_URL:
# Parse the REDIS_URL to extract host and port
parsed_url = urlparse(REDIS_URL)
redis_host = parsed_url.hostname
redis_port = parsed_url.port
logging.info(f"Using Redis host: {redis_host} and port: {redis_port}")
else:
# Fallback in case REDIS_URL is not set
redis_host = ‘127.0.0.1’
redis_port = 6379
logging.warning(“REDIS_URL environment variable not set, using fallback values”)
CHANNEL LAYERS setup
CHANNEL_LAYERS = {
‘default’: {
‘BACKEND’: ‘channels_redis.core.RedisChannelLayer’,
‘CONFIG’: {
‘hosts’: [(redis_host, redis_port)],
}
}
}
my backend logs seem to indicate the connection is going through:
==> Deploying…
==> Running ‘daphne -b 0.0.0.0 -p 8000 mysite.asgi:application’
2025-04-22 19:42:50,585 INFO Using Redis host: red-d03sqi9r0fns739g5cng and port: 6379
If anyone has any insight into this, your help would be greatly appreciated. Happy to share further. Thanks in advance.