Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
channel_layer.send not calling handler

I have this signal to dispatch an event when a new message is created

from django.db.models.signals import post_save
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
from django.dispatch import receiver

from .serializers import MessageSerializer
from base.models import Message

@receiver(post_save, sender=Message)
def new_message(sender, instance, created, **kwargs):
    channel_layer = get_channel_layer()

    if created:
        chat_group = instance.channel.chat_group
        for member in chat_group.members:
            async_to_sync(channel_layer.send)(
                f"User-{member.user.id}",
                {
                    "type": "chat_message_create",
                    "payload": MessageSerializer(instance).data,
                },
            )

this is the consumers.py

import json
from channels.generic.websocket import WebsocketConsumer

from api.serializers import UserSerializer


class ChatConsumer(WebsocketConsumer):
    def connect(self):
        user = self.scope["user"]
        self.channel_name = f"User-{user.id}"
        self.send(text_data=json.dumps(UserSerializer(user).data))

    def disconnect(self, close_code):
        pass

    def chat_message_create(self, event):
        print("Created")
        self.send(text_data=json.dumps(event["payload"]))

When I create a new message in the admin panel the signal gets called but it does not get dispatched through the websocket, and any debug prints in ChatConsumer.chat_message_create won't even get printed.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Fixed the issue, I reworked the Consumers to use Groups instead of single channels

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!