Django Channel Connection Refused in Production

Hi,

I’m currently implementing Django Channels for asynchronous communication. The setup works flawlessly on my local environment, but when deployed in production (Docker container), I encounter a “connection refused” error for WebSocket connections.

Could you please review my configuration and advise on any potential issues? Your insights would be greatly appreciated.

asgi.py
"""
ASGI config for sewts_jupiter_cloud_backend project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
"""

import os

# from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
# from channels.security.websocket import AllowedHostsOriginValidator
from django.core.asgi import get_asgi_application

import channel.urls

setting_evironment = os.getenv('ENVIRONMENT')
os.environ.setdefault('DJANGO_SETTINGS_MODULE',
                      f'sewts_jupiter_cloud_backend.settings.{setting_evironment}')

application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    # TODO: apply allowed host check here
    "websocket": URLRouter(channel.urls.websocket_urlpatterns)

})



setting
ALLOWED_HOSTS=192.168.108.76,ws://192.168.108.76
CORS_ALLOWED_ORIGINS=http://192.168.108.76:8080,http://192.168.108.76:8001,http://192.168.108.76:8000

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer",  # This uses an in-memory layer
        "CONFIG": {},
    },
}

INSTALLED_APPS = [
    'daphne',
    'channels',
-----




ERROR on browser

Welcome @muhammad-sewts !

We’re going to need to know a lot more details about your project and your deployment environment.

  • What versions of Django, Channels, and Python are you using?

  • What web server are you using?

    • How is it configured for handling the web sockets?
  • How are you running Daphne? (What is the command being used to start it?)

  • What is the JavaScript code that you are using to connect to the websocket?

This is bad. Change to an appropriate channel layer (redis).

This all is just a starting point, we’re likely to have more questions based upon your answers to these.