Hello everyone, I am just following the tutorials on the channels 2, using daphane async from django channels documentation.
where everything works just fine as the documentation untill I tried redis with it. I have follow the exact coding as per the documentation then I get the below errors when connecting to websocket with redis.
This is how I have installed redis in my windows docker
docker run --name redis -d -p 6379:6379 --rm redis
given that I am able to ping redis using redis-cli
Here are my settings.py config
CHANNEL_LAYERS = {
# "default": {
# "BACKEND": "channels.layers.InMemoryChannelLayer"
# },
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
# "hosts": [("redis", 6379)],
# "hosts": [("localhost", 6379)],
"hosts": [("127.0.0.1", 6379)],
# "hosts": ["redis://redis:6379/1"],
},
},
}
My consumer.py exactly same as in the documentation tutorial
# chat/consumers.py
import json
from channels.generic.websocket import AsyncWebsocketConsumer
class ChatConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
self.room_group_name = f"chat_{self.room_name}"
# Join room group
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
await self.accept()
async def disconnect(self, close_code):
# Leave room group
await self.channel_layer.group_discard(self.room_group_name, self.channel_name)
# Receive message from WebSocket
async def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json["message"]
# Send message to room group
await self.channel_layer.group_send(
self.room_group_name, {"type": "chat.message", "message": message}
)
# Receive message from room group
async def chat_message(self, event):
message = event["message"]
# Send message to WebSocket
await self.send(text_data=json.dumps({"message": message}))
this is the error
WebSocket HANDSHAKING /ws/chat/anonymous/7f2e3798_bf6a_440c_b3a1_3e73ae6bb4af/ [127.0.0.1:61238]
Room Name: 7f2e3798_bf6a_440c_b3a1_3e73ae6bb4af
Exception inside application: 'RedisChannelLayer' object has no attribute 'valid_channel_name'
Traceback (most recent call last):
File "C:\Users\JoydipNath(RAPP)\AppData\Local\Programs\Python\Python311\Lib\site-packages\channels\utils.py", line 50, in await_many_dispatch
await dispatch(result)
File "C:\Users\JoydipNath(RAPP)\AppData\Local\Programs\Python\Python311\Lib\site-packages\channels\consumer.py", line 74, in dispatch
await handler(message)
File "C:\Users\JoydipNath(RAPP)\AppData\Local\Programs\Python\Python311\Lib\site-packages\channels\generic\websocket.py", line 180, in websocket_connect
await self.connect()
File "C:\Users\JoydipNath(RAPP)\PycharmProjects\ClearMyMind\RTCHAT\utils\anonymous_consumer.py", line 19, in connect
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
File "C:\Users\JoydipNath(RAPP)\AppData\Local\Programs\Python\Python311\Lib\site-packages\channels_redis\core.py", line 485, in group_add
assert self.valid_group_name(group), "Group name not valid"
^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'RedisChannelLayer' object has no attribute 'valid_group_name'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\JoydipNath(RAPP)\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\staticfiles\handlers.py", line 101, in __call__
return await self.application(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\JoydipNath(RAPP)\AppData\Local\Programs\Python\Python311\Lib\site-packages\channels\routing.py", line 48, in __call__
return await application(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\JoydipNath(RAPP)\AppData\Local\Programs\Python\Python311\Lib\site-packages\channels\security\websocket.py", line 37, in __call__
return await self.application(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\JoydipNath(RAPP)\AppData\Local\Programs\Python\Python311\Lib\site-packages\channels\sessions.py", line 44, in __call__
return await self.inner(dict(scope, cookies=cookies), receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\JoydipNath(RAPP)\AppData\Local\Programs\Python\Python311\Lib\site-packages\channels\sessions.py", line 261, in __call__
return await self.inner(wrapper.scope, receive, wrapper.send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\JoydipNath(RAPP)\AppData\Local\Programs\Python\Python311\Lib\site-packages\channels\auth.py", line 185, in __call__
return await super().__call__(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\JoydipNath(RAPP)\AppData\Local\Programs\Python\Python311\Lib\site-packages\channels\middleware.py", line 24, in __call__
return await self.inner(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\JoydipNath(RAPP)\AppData\Local\Programs\Python\Python311\Lib\site-packages\channels\routing.py", line 118, in __call__
return await application(
^^^^^^^^^^^^^^^^^^
File "C:\Users\JoydipNath(RAPP)\AppData\Local\Programs\Python\Python311\Lib\site-packages\channels\consumer.py", line 95, in app
return await consumer(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\JoydipNath(RAPP)\AppData\Local\Programs\Python\Python311\Lib\site-packages\channels\consumer.py", line 58, in __call__
await await_many_dispatch(
Package versions
Django==5.1.5
channels==4.2.1
daphne==4.1.2
channels-redis==4.2.1
hiredis==3.1.0
django-redis==5.4.0
It will be great if I can get some help from community, looking forward to it.
Thank you