Bird
0
0

You want to broadcast a message to all clients in a chat room using Django Channels. Which approach correctly sends a message to the group named "chat_room"?

hard📝 Application Q15 of 15
Django - Async Django
You want to broadcast a message to all clients in a chat room using Django Channels. Which approach correctly sends a message to the group named "chat_room"?
Aawait self.send_group("chat_room", {"text": "Hello"})
Bawait self.group_send("chat_room", {"type": "chat.message", "text": "Hello"})
Cself.channel_layer.send_group("chat_room", {"text": "Hello"})
Dawait self.channel_layer.group_send("chat_room", {"type": "chat.message", "text": "Hello"})
Step-by-Step Solution
Solution:
  1. Step 1: Recall group message syntax

    Use channel_layer.group_send with await and a message dict including "type" key.
  2. Step 2: Check options

    await self.channel_layer.group_send("chat_room", {"type": "chat.message", "text": "Hello"}) uses correct method, await, and message format. Others use invalid methods or missing await.
  3. Final Answer:

    await self.channel_layer.group_send("chat_room", {"type": "chat.message", "text": "Hello"}) -> Option D
  4. Quick Check:

    Group send = await channel_layer.group_send(...) [OK]
Quick Trick: Use await channel_layer.group_send with type key [OK]
Common Mistakes:
MISTAKES
  • Using non-existent send_group or group_send methods
  • Omitting await on async calls
  • Missing the required "type" key in message dict

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes