django / channels

Developer-friendly asynchrony for Django

Home Page:https://channels.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AttributeError: 'NoneType' object has no attribute 'group_add'

Pedr0Rodrigues opened this issue · comments

Following each step of the tutorial available at https://channels.readthedocs.io/en/stable/tutorial/part_2.html, at the end of part 2 I get to this error message. I saw comments of a similar error at #1036, but I was not successful. Any help would be most welcome, thanks!


(Casca) pafr:~/anaconda3/envs/Casca/Teste/casca$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
January 20, 2022 - 13:30:52
Django version 4.0.1, using settings 'casca.settings'
Starting ASGI/Channels version 3.0.4 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
HTTP GET / 200 [0.01, 127.0.0.1:50904]
HTTP GET /favicon.ico/ 200 [0.01, 127.0.0.1:50904]
HTTP GET /chat/10/ 200 [0.01, 127.0.0.1:50904]
WebSocket HANDSHAKING /ws/chat/10/ [127.0.0.1:50906]
HTTP GET /favicon.ico/ 200 [0.00, 127.0.0.1:50904]
Exception inside application: 'NoneType' object has no attribute 'group_add'
Traceback (most recent call last):
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/channels/staticfiles.py", line 44, in __call__
    return await self.application(scope, receive, send)
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/channels/routing.py", line 71, in __call__
    return await application(scope, receive, send)
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/channels/sessions.py", line 47, in __call__
    return await self.inner(dict(scope, cookies=cookies), receive, send)
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/channels/sessions.py", line 263, in __call__
    return await self.inner(wrapper.scope, receive, wrapper.send)
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/channels/auth.py", line 185, in __call__
    return await super().__call__(scope, receive, send)
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/channels/middleware.py", line 26, in __call__
    return await self.inner(scope, receive, send)
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/channels/routing.py", line 150, in __call__
    return await application(
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/channels/consumer.py", line 94, in app
    return await consumer(scope, receive, send)
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/channels/consumer.py", line 62, in __call__
    await await_many_dispatch([receive], self.dispatch)
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/channels/utils.py", line 51, in await_many_dispatch
    await dispatch(result)
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/asgiref/sync.py", line 444, in __call__
    ret = await asyncio.wait_for(future, timeout=None)
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
    return await fut
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/concurrent/futures/thread.py", line 52, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/channels/db.py", line 13, in thread_handler
    return super().thread_handler(loop, *args, **kwargs)
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/asgiref/sync.py", line 486, in thread_handler
    return func(*args, **kwargs)
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/channels/consumer.py", line 125, in dispatch
    handler(message)
  File "/home/pafr/anaconda3/envs/Casca/lib/python3.9/site-packages/channels/generic/websocket.py", line 38, in websocket_connect
    self.connect()
  File "/home/pafr/anaconda3/envs/Casca/Teste/casca/chat/consumers.py", line 11, in connect
    async_to_sync(self.channel_layer.group_add)(
AttributeError: 'NoneType' object has no attribute 'group_add'
WebSocket DISCONNECT /ws/chat/10/ [127.0.0.1:50906]

And my consumers.py file, just like the tutorial.

import json
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer

class ChatConsumer(WebsocketConsumer):
    def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name

        # Join room group
        async_to_sync(self.channel_layer.group_add)(
            self.room_group_name,
            self.channel_name
        )

        self.accept()

    def disconnect(self, close_code):
        # Leave room group
        async_to_sync(self.channel_layer.group_discard)(
            self.room_group_name,
            self.channel_name
        )

    # Receive message from WebSocket
    def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']

        # Send message to room group
        async_to_sync(self.channel_layer.group_send)(
            self.room_group_name,
            {
                'type': 'chat_message',
                'message': message
            }
        )

    # Receive message from room group
    def chat_message(self, event):
        message = event['message']

        # Send message to WebSocket
        self.send(text_data=json.dumps({
            'message': message
        }))

commented

In that issue it was mentioned the problem was in settings.CHANNEL_LAYERS. How does this settings value looks like in your case?