Search code examples
djangowebsocketredisdjango-channels

django channels redis took too long to shut down and was killed


so i was trying to do some basic stuff with channels. i wrote a script in my html to return a message

script

<script>
        const chatsocket = new WebSocket(
            'ws://'+window.location.host+'/ws/test/'
        )
        chatsocket.onmessage = function (e) {
            const data = JSON.parse(e.data)
            console.log(data)
            chatsocket.close()
        }
    </script>

my consumers.py

from channels.exceptions import StopConsumer
from channels.generic.websocket import AsyncWebsocketConsumer
import json


class NotificationConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        self.group_name = 'notificationgroup'
        await self.channel_layer.group_add(
            self.group_name,
            self.channel_name
        )
        await self.accept()

        await self.channel_layer.group_send(
            self.group_name,
            {
                'type': 'tester_message',
                'tester': 'hello world'
            }
        )

    async def tester_message(self, event):
        tester = event['tester']
        await self.send(text_data=json.dumps({
            'tester': tester
        }))

    async def disconnect(self, close_code):
        await self.channel_layer.group_discard(
            self.group_name,
            self.channel_name
        )
        raise StopConsumer()

so basically i get my desired output in the console... when i have

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

but when i use an aws redis cluster (after installing channels-redis obviously)

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

i get this error

WebSocket HANDSHAKING /ws/test/ [127.0.0.1:51551]
WebSocket DISCONNECT /ws/test/ [127.0.0.1:51551]
Application instance <Task pending name='Task-4' coro=<StaticFilesWrapper.__call__() running at E:\intes\sync\shero\venv\lib\site-packages\channels\staticfiles.py:44> wait_for=<Future pending cb=[BaseSelectorEventLoop._sock_write_done(1216)(), <TaskWakeupMethWrapper object at 0x000001F7D5226C70>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 51551] path=b'/ws/test/'> took too long to shut down and was killed.

what am I doing wrong?


Solution

  • I was doing it all wrong. Apparently, you cannot get the aws-redis working outside. you must have your application running on an EC2 within the same VPN. Then, it works!