Django: the Celery signal, Redis Channel and AsyncWebsocket stack is not working

I'm trying to trigger a WebSocket function from my celery Signal using redis channels.

So this is my AsyncWebsocket:

class Consumer(AsyncWebsocketConsumer):

        
    async def connect(self):
        self.room_group_name = 'test'  # Ensure consistent group name
            
        await self.channel_layer.group_add(self.room_group_name, self.channel_name)
        print(f"Consumer {self.channel_name} joined group {self.room_group_name}")  # Debugging line

            
        await self.accept()
            

    
    def chat_message(self, event):
        message = event['message']
        print(f"Chat message received: {message}")  
        self.send(text_data=json.dumps({
            'type':'chat',
            'message':message
        }))

This is my Signal:

async def send_chat_message(task_id, message):
    channel_layer = get_channel_layer()
    group_name = f"teste"

    # Send message to the WebSocket group
    await channel_layer.group_send(
        group_name,
        {
            'type': 'chat_message',
            'message': message
        }
    )

@task_success.connect
def task_success_handler(sender, result, **kwargs):


    message = f"Task {task_id} has completed successfully!"
    async_to_sync(send_chat_message)(task_id, message)
    print(message)

And this is my redis config:

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

Note: I'm using docker, thats why host is "redis" and also I'm sure my django container can comunicate with the Redis container because when I use the Consumer, the redis monitor outputs:

1731094047.426342 [0 192.168.96.6:47094] "EVAL" "\n            local backed_up = redis.call('ZRANGE', ARGV[2], 0, -1, 'WITHSCORES')\n            for i = #backed_up, 1, -2 do\n                redis.call('ZADD', ARGV[1], backed_up[i], backed_up[i - 1])\n            end\n            redis.call('DEL', ARGV[2])\n        " "0" "asgispecific.d7702f4e3ed345e39497687e16c7ebd5!" "asgispecific.d7702f4e3ed345e39497687e16c7ebd5!$inflight"
1731094047.426476 [0 lua] "ZRANGE" "asgispecific.d7702f4e3ed345e39497687e16c7ebd5!$inflight" "0" "-1" "WITHSCORES"
1731094047.426485 [0 lua] "DEL" "asgispecific.d7702f4e3ed345e39497687e16c7ebd5!$inflight"
1731094047.426685 [0 192.168.96.6:47094] "BZPOPMIN" "asgispecific.d7702f4e3ed345e39497687e16c7ebd5!" "5"
1731094047.435198 [0 192.168.96.6:47104] "ZADD" "asgi:group:test" "1731094047.433636" "specific.d7702f4e3ed345e39497687e16c7ebd5!565251dda3294a369190ccbad0eb8515"
1731094047.435539 [0 192.168.96.6:47104] "EXPIRE" "asgi:group:test" "86400" 

And when I send the message aka call the function in manage.py shell:

1731094160.374366 [0 192.168.96.6:42798] "ZREMRANGEBYSCORE" "asgi:group:teste" "0" "1731007760"
1731094160.375050 [0 192.168.96.6:42798] "ZRANGE" "asgi:group:teste" "0" "-1"

So my question is why is chat_message not getting triggered?

I tried debugging with prints but the only one not getting printed is the print(f"Chat message received: {message}") from Consumer.

I tried the redis-cli monitor, to check if I was receiving connections or not.

I'm I using the correct configuration?

INSTALLED_APPS = [
    "daphne",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django_celery_beat",
    "django_extensions",
    "rest_framework_simplejwt.token_blacklist",
    "drf_yasg",
    "apps.common",
    "apps.user_app",
    "apps.task_app",
    "apps.api",
    
]
Вернуться на верх