Django Channels: Channel Name type error appears when loading web socket chat

Question:

I’m building a basic websocket live chat using Django Channels and while the site loads fine, when I enter into the site, in my terminal I get this TypeError Message:

TypeError: Channel name must be a valid unicode string with length < 100 containing only ASCII alphanumerics, hyphens, underscores, or periods, not RedisChannelLayer(hosts=[{'host': '127.0.0.1', 'port': 6379}])

Im assuming the problem lies in my settings.py file but based on my current channel layer settings, I don’t see what the problem is:

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

I’m new to django and channels so maybe there’s a place where I defined the channel name and i’m just missing but I’m not sure or where to look for it if thats true.

This is my Full traceback error and consumer.py file btw:

Exception inside application: Channel name must be a valid unicode string with length < 100 containing only ASCII alphanumerics, hyphens, underscores, or periods, not RedisChannelLayer(hosts=[{'host': '127.0.0.1', 'port': 6379}])
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/contrib/staticfiles/handlers.py", line 101, in __call__
    return await self.application(scope, receive, send)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/channels/routing.py", line 62, in __call__
    return await application(scope, receive, send)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/channels/security/websocket.py", line 37, in __call__
    return await self.application(scope, receive, send)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/channels/sessions.py", line 47, in __call__
    return await self.inner(dict(scope, cookies=cookies), receive, send)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/channels/sessions.py", line 263, in __call__
    return await self.inner(wrapper.scope, receive, wrapper.send)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/channels/auth.py", line 185, in __call__
    return await super().__call__(scope, receive, send)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/channels/middleware.py", line 24, in __call__
    return await self.inner(scope, receive, send)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/channels/routing.py", line 116, in __call__
    return await application(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/channels/consumer.py", line 94, in app
    return await consumer(scope, receive, send)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/channels/consumer.py", line 58, in __call__
    await await_many_dispatch(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/channels/utils.py", line 50, in await_many_dispatch
    await dispatch(result)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/channels/consumer.py", line 73, in dispatch
    await handler(message)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/channels/generic/websocket.py", line 173, in websocket_connect
    await self.connect()
  File "/Users/alecsmith/Documents/Python Work/ChatApp/chat/consumers.py", line 10, in connect
    await self.channel_layer.group_add(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/channels_redis/core.py", line 518, in group_add
    assert self.valid_channel_name(channel), "Channel name not valid"
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/channels/layers.py", line 160, in valid_channel_name
    raise TypeError(self.invalid_name_error.format("Channel", name))
TypeError: Channel name must be a valid unicode string with length < 100 containing only ASCII alphanumerics, hyphens, underscores, or periods, not RedisChannelLayer(hosts=[{'host': '127.0.0.1', 'port': 6379}])
import json
from channels.generic.websocket import AsyncWebsocketConsumer

# Class tells websocket how it should be used when doing real time events betweeen computer and server
class ChatConsumer(AsyncWebsocketConsumer):
    # Accepts the connection of the websocket, makes a group name for the chatroom,
    # and add the group to the channel layer
    async def connect(self):
        self.roomGroupName = 'group_chat_gfg'
        await self.channel_layer.group_add(
            self.roomGroupName,
            self.channel_layer
        )
        await self.accept()
    
    # Removes the group name and removes the group itself from the channel layer
    async def disconnect(self):
        await self.channel_layer.group_discard(
            self.roomGroupName,
            self.channel_layer
        )
    
    # Recieves the message and spreads it to all other users in the chat room
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        username = text_data_json['username']
        await self.channel_layer.group_send(
            self.roomGroupName, {
                'type': 'sendMessage',
                'message': message,
                'username': username
            }
        )
    
    # Takes the name of the user and their message and sends it to the chat room
    async def sendMessage(self, event):
        message = event['message']
        username = event['username']
        await self.send(text_data = json.dumps({'message': message, 'username': username}))
Asked By: Alec Smith

||

Answers:

You should pass self.channel_name to group_add() and not self.channel_layer

    async def connect(self):
        self.roomGroupName = 'group_chat_gfg'
        await self.channel_layer.group_add(
            self.roomGroupName,
            self.channel_name # here
        )
        await self.accept()

    async def disconnect(self):
        await self.channel_layer.group_discard(
            self.roomGroupName,
            self.channel_name # and here
        )
Answered By: Iain Shelvington