0
0
Djangoframework~10 mins

Channels for WebSocket support in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Channels layer for WebSocket support.

Django
from channels import [1]
Drag options to blanks, or click blank then click option'
Aconsumers
Blayers
Crouting
Dwebsocket
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'layers' instead of 'routing'
Trying to import 'websocket' directly
Confusing 'consumers' with routing
2fill in blank
medium

Complete the code to define a WebSocket consumer class inheriting from the correct Channels base class.

Django
from channels.generic.websocket import [1]

class ChatConsumer([1]):
    pass
Drag options to blanks, or click blank then click option'
AAsyncWebsocketConsumer
BWebsocketHandler
CWebsocketConsumer
DAsyncConsumer
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'WebsocketHandler' which does not exist
Using synchronous 'WebsocketConsumer' when async is preferred
Using 'AsyncConsumer' which is more generic
3fill in blank
hard

Fix the error in the routing configuration by completing the WebSocket URL pattern.

Django
from django.urls import re_path
from . import consumers

websocket_urlpatterns = [
    re_path(r'ws/chat/(?P<[1]>[^/]+)/$', consumers.ChatConsumer.as_asgi()),
]
Drag options to blanks, or click blank then click option'
Aroom_name
Broomname
Croom-name
Droom
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'roomname' without underscore
Using hyphenated 'room-name' which is invalid in regex group names
Using 'room' which may not match consumer code
4fill in blank
hard

Fill both blanks to complete the async method that accepts a WebSocket connection and adds the user to a group.

Django
async def connect(self):
    self.room_group_name = f'chat_[1]'
    await self.channel_layer.[2](
        self.room_group_name,
        self.channel_name
    )
    await self.accept()
Drag options to blanks, or click blank then click option'
Aroom_name
Bsend
Cgroup_add
Dgroup_send
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'group_add' for channel layer method
Using 'group_send' which sends messages, not adds channels
Using wrong variable name for room identifier
5fill in blank
hard

Fill all three blanks to complete the async method that receives a message and sends it to the group.

Django
async def receive(self, text_data):
    text_data_json = json.loads(text_data)
    message = text_data_json['message']

    await self.channel_layer.[1](
        self.room_group_name,
        {
            'type': '[2]',
            'message': message
        }
    )

async def chat_message(self, event):
    message = event['message']

    await self.send(text_data=json.dumps([3]))
Drag options to blanks, or click blank then click option'
Agroup_send
Bchat_message
C{'message': message}
Dgroup_add
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'group_add' instead of 'group_send'
Mismatching the 'type' string with the handler method name
Sending raw message string instead of a dictionary