import json from channels.generic.websocket import WebsocketConsumer from asgiref.sync import async_to_sync from waiting_room.tasks import calculate_min_distance class MyConsumer(WebsocketConsumer): def connect(self): self.room_group_name = self.scope["url_route"]["kwargs"]["room_name"] async_to_sync(self.channel_layer.group_add)(self.room_group_name, self.channel_name) self.accept() calculate_min_distance(self.room_group_name) def disconnect(self, close_code): async_to_sync(self.channel_layer.group_discard)(self.room_group_name, self.channel_name) self.close() def receive(self, text_data): # Echo self.send(text_data=text_data) def channel_message(self, event): message = event['message'] # Send message to WebSocket self.send(text_data=str(message))