Performance: Channels for WebSocket support
This affects the responsiveness and real-time interaction speed of web applications using WebSockets.
Using Django Channels with asynchronous consumers to handle WebSocket connections efficiently.Using synchronous Django views to handle WebSocket connections without Channels, e.g., handling WebSocket in standard HTTP views.| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous WebSocket handling in Django views | N/A | N/A | N/A | [X] Bad |
| Asynchronous WebSocket handling with Django Channels | N/A | N/A | N/A | [OK] Good |
class ChatConsumer(AsyncWebsocketConsumer):
async def receive(self, text_data):
data = json.loads(text_data)
response = {"reply": data["text"].upper()}
await self.send(text_data=json.dumps(response))class MyConsumer(AsyncWebsocketConsumer):
async def connect(self):
await self.accept
async def receive(self, text_data):
await self.send(text_data=text_data)Pass the test and this pattern moves unknown