0

I wrote these codes with Django Channels, but it gives me an error: These codes used to work correctly, but I don't know why they went wrong.

    def receive(self, text_data=None, bytes_data=None):
        text_data_json = json.loads(text_data)
        message_type = text_data_json["type"]
        message = text_data_json["message"]
        if message_type == 'providerToAdmin':
            chat = Chats.objects.get(user=self.sender)
            Message.objects.create(text=message, date=datetime.datetime.now(), user_type=0, chat=chat)
            group_name = f'chat_{self.admin.username}'
            # self.send(text_data=json.dumps({'check': True, 'message': message}))
            async_to_sync(self.channel_layer.group_send)(
                group_name, {"userType": 0, "data": message, 'chatId': chat.id}
            )

        if message_type == 'adminToUsers':
            chat = Chats.objects.get(user=self.sender)
            Message.objects.create(text=message, date=datetime.datetime.now(), user_type=1, chat=chat)
            group_name = f'chat_{self.admin.username}'
            async_to_sync(self.channel_layer.group_send)(
                group_name, {"userType": 0, "data": message, 'chatId': chat.id}
            )

        if message_type == 'providerToStore':
            receiver = text_data_json["receiver"]
            receiver = Store.objects.get(user__username=receiver)
            provider = Provider.objects.get(user__username=self.sender)
            chat = ChatsSP.objects.get(store=receiver, provider=provider)
            MessageSP.objects.create(text=message, date=datetime.datetime.now(), user_type=1, chat=chat)
            group_name = f'chat_{receiver.user.username}'
            async_to_sync(self.channel_layer.group_send)(
                group_name, {"userType": 0, "data": message, 'chatId': chat.id}
            )

    def chat_message(self, event):
        message = event["data"]
        name = event["name"]
        userName = event["userName"]
        chatId = event["chatId"]
        self.send(text_data=json.dumps({"message": message, 'name': name, 'userName': userName,
                                        'chatId': chatId
                                        }))

but it raised this error:

aise ValueError("Incoming message has no 'type' attribute")

1 Answer 1

0

I encountered the same issue today, and after some debugging, I found the solution. The problem is that Channels expects a key called type in the message you're sending. This key is used to route the message to the correct handler within your consumer.

In your current code, you're passing a key like userType, but Channels doesn't recognize that key for routing purposes. Instead, you need to include a type key that maps to a method in your consumer.

Here's an example:

{"type": "providerToStore", "data": message, 'chatId': chat.id}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.