Bird
0
0

Given this Django ASGI consumer code snippet, what will happen when a WebSocket connection is accepted?

medium📝 component behavior Q13 of 15
Django - Async Django
Given this Django ASGI consumer code snippet, what will happen when a WebSocket connection is accepted?
from channels.generic.websocket import AsyncWebsocketConsumer

class ChatConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        await self.accept()
AThe server will crash due to missing HTTP response.
BThe WebSocket connection is accepted and ready to receive messages asynchronously.
CThe connection will be rejected because accept() is synchronous.
DNothing happens because connect() is not called automatically.
Step-by-Step Solution
Solution:
  1. Step 1: Understand AsyncWebsocketConsumer behavior

    The connect method is called automatically on WebSocket connection attempts.
  2. Step 2: Analyze the accept() call

    Calling await self.accept() accepts the WebSocket connection asynchronously, enabling message exchange.
  3. Final Answer:

    The WebSocket connection is accepted and ready to receive messages asynchronously. -> Option B
  4. Quick Check:

    Async accept() means connection accepted [OK]
Quick Trick: Async accept() means WebSocket connection is accepted [OK]
Common Mistakes:
MISTAKES
  • Thinking accept() is synchronous
  • Assuming connect() is not called automatically
  • Confusing WebSocket with HTTP requests

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes