# chat/consumers.py import json from account.models import UserProfile from channels.db import database_sync_to_async from channels.generic.websocket import AsyncWebsocketConsumer class ChatConsumer(AsyncWebsocketConsumer): async def connect(self): user = self.scope["user"] if not user.is_authenticated: await self.close(code=4401) # unauthorized return await self.accept() async def disconnect(self, close_code): self.disconnect() pass async def receive(self, data): if data["type"] == "chat_message": pass else: self.close(reason="Unsupported message type") # -- CUSTOM METHODS -- async def send_message_to_chat_group(self, event): message = event["message"] await create_new_message() await self.send(text_data=json.dumps({"message": message})) @database_sync_to_async def create_new_message(user_id): return None