import logging from json.decoder import JSONDecodeError from json import loads as json_loads from json import dumps as json_dumps from channels.generic.websocket import AsyncWebsocketConsumer class NotifyConsumer(AsyncWebsocketConsumer): def __init__(self, *args, **kwargs): super().__init__(args, kwargs) self.log = logging.getLogger("server.log") self.room_group_name = "general" async def connect(self): # Join room group await self.channel_layer.group_add(self.room_group_name, self.channel_name) self.log.info(f"Added {self.channel_name} channel to {self.room_group_name} group") await self.accept() async def disconnect(self, close_code): # Leave room group await self.channel_layer.group_discard(self.room_group_name, self.channel_name) # Receive message from WebSocket async def receive(self, text_data=None, bytes_data=None): self.log.info(f"Received message: {text_data}") try: text_data_json = json_loads(text_data) message = text_data_json["message"] # Send message to room group await self.channel_layer.group_send( self.room_group_name, {"type": "generic.event", "message": message, "name": "send_message_to_frontend", "event_id": 1} ) except JSONDecodeError as e: await self.send(text_data=json_dumps({"message": "error", "error": "malformed json"})) self.log.error(e) except KeyError as e: await self.send(text_data=json_dumps({"message": "error", "error": f"missing key: {str(e)}"})) self.log.error(e) except Exception as e: await self.send(text_data=json_dumps({"message": "error", "error": "unknown error"})) self.log.error(e) raise e # Receive message from room group async def generic_event(self, event): self.log.info(f"Received event: {event}") message = event["message"] name = event["name"] event_id = event["event_id"] # Send message to WebSocket await self.send(text_data=json_dumps({"message": message, "name": name, "event_id": event_id}))