python-3.xdjangowebsocketdjango-channelsdjango-3.2

Django Channels Consumer Not Connecting to websocket


i created a websocket with javascripts on the client side...then followed up with setting up my project to handle webocket connections as follows(following the official django-channels documentation). But each time i refresh the page and watch the websocket from the browser console...its fails. i inserted an print statement in the init of the consumer class and it was printed(each time a page containing a websocket was visited or refreshed)..which mean that the routing is working fine...but for some reasons the consumer is not connecting/accepting the connection as expected to the websocket. and again there is no log in the development server as to any websocket connection process.Please can anyone help and suggest a fix.

python-version - 3.9.9, django-version - 3.2.9, channels-version - 3.0.4

my setting.py file(relevant lines)
INSTALLED_APPS = [
    ...
    'channels',
]


ASGI_APPLICATION = 'ma_mall.asgi.application'

CHANNEL_LAYERS = {
    "default": {
        'BACKEND': "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            'hosts': [('127.0.0.1', 6379)],
        }
    }
}

my asgi.py file
application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": URLRouter(
            routing.websocket_urlpatterns            
        )
})

my routing.py file

websocket_urlpatterns = [
    path("ws/notifications/", consumers.NotificationConsumer.as_asgi()),
]
my consumer file
class NotificationConsumer(AsyncJsonWebsocketConsumer):
    groups = ['general_group']

    async def connect(self):
        await self.accept()
        await self.channel_layer.group_add('notification_group', self.channel_name)
        await self.channel_layer.group_send('notification_group', 
            {
                'type': 'tester.message',
                'tester': 'tester'
            }
        )
        
    async def disconnect(self, close_code):
        await self.channel_layer.group_discard('notification_group', self.channel_name)

the javascript for the websocket on the client-side

i created as websocket on the client side using javascript as follows

       const notification_websocket = new WebSocket(
            'ws://' +
            window.location.host +
            '/ws/notifications/'
        );

        notification_websocket.onmessage = function (e) {
            let data = JSON.parse(e.data);
            console.log("Just received this from the back end.. 0", data);
        }

        notification_websocket.onclose = function (e) {
            console.log("websocket closed");
        }

Solution

  • i realized that i had used an Older Version of Redis (to go around using redis on Windows) and the issue happen to be coming from the redis part of the configuration. Once i switched from using Redis to using a Memcache

    CHANNEL_LAYERS = {
        "default": {
            "BACKEND": "channels.layers.InMemoryChannelLayer"
        }
    }
    

    the websocket-consumer connection connected and was kept open as expected... i hope to find a solution to using Redis newer version on Windows for development