Problems with django channels sending messages to multiple clients

the problem is that channels is used as an extension of django to support websocket. The front-end page sends data to the backend through websocket, routes to consumer processing, and sends back messages to the front-end after processing. It is perfect, but when the same page is opened in multiple tabs (or open), consumer on multiple computers, it can receive data sent by multiple pages, and the data is the same. However, when sending the returned data, only the first client can receive it, and the later open page will not receive the message. Why?

consumer example code I will post the official example. Except that there is no code to handle the task, the other code is basically the same as mine:

-sharp chat/consumers.py
from channels.generic.websocket import AsyncWebsocketConsumer
import json

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

        -sharp Join room group
        await self.channel_layer.group_add(
            self.room_group_name,
            self.channel_name
        )

        await self.accept()

    async def disconnect(self, close_code):
        -sharp Leave room group
        await self.channel_layer.group_discard(
            self.room_group_name,
            self.channel_name
        )

    -sharp Receive message from WebSocket
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json["message"]

        -sharp Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name,
            {
                "type": "chat_message",
                "message": message
            }
        )

    -sharp Receive message from room group
    async def chat_message(self, event):
        message = event["message"]

        -sharp Send message to WebSocket
        await self.send(text_data=json.dumps({
            "message": message
        }))

problem description

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

problem description

the platform version of the problem and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

problem description

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

Oct.12,2021
Have you set CHANNEL_LAYER in

settings.py

Menu