Bird
0
0

Given this consumer code snippet, what will be printed when a client sends a text message 'hello'?

medium📝 component behavior Q4 of 15
Django - Async Django
Given this consumer code snippet, what will be printed when a client sends a text message 'hello'? ```python class EchoConsumer(AsyncWebsocketConsumer): async def connect(self): await self.accept() async def receive(self, text_data): await self.send(text_data=text_data) ```
AThe server sends back 'hello' to the client
BThe server closes the connection immediately
CThe server sends back an empty message
DThe server raises an error because send is missing
Step-by-Step Solution
Solution:
  1. Step 1: Analyze receive method behavior

    When a message is received, the consumer sends back the same text_data to the client.
  2. Step 2: Confirm connection acceptance

    The connect method calls accept(), so connection is open and ready to send/receive.
  3. Final Answer:

    The server sends back 'hello' to the client -> Option A
  4. Quick Check:

    EchoConsumer sends received text back [OK]
Quick Trick: EchoConsumer sends back received messages immediately [OK]
Common Mistakes:
MISTAKES
  • Assuming connection is closed without accept()
  • Thinking send method is missing
  • Expecting empty message response

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes