django / channels

Developer-friendly asynchrony for Django
https://channels.readthedocs.io
BSD 3-Clause "New" or "Revised" License
6.08k stars 801 forks source link

error in channel layer explanations #2065

Closed Twintersh closed 9 months ago

Twintersh commented 9 months ago
await self.channel_layer.group_send(
    room.group_name,
    {
        "type": "chat.message",
        "room_id": room_id,
        "username": self.scope["user"].username,
        "message": message,
    }
)

should be

await self.channel_layer.group_send(
    room.group_name,
    {
        "type": "chat_message",
        "room_id": room_id,
        "username": self.scope["user"].username,
        "message": message,
    }
)

it is not calling the right function name (chat.message -> chat_message)

carltongibson commented 9 months ago

The . is mapped to _ by the consumer when determining which method to call, so the chat.message format is correct, just as http.request works for ASGI, mapping to http_request.

carltongibson commented 9 months ago

From the docs:

Consumers are structured around a series of named methods corresponding to the type value of the messages they are going to receive, with any . replaced by _.