Search code examples
javascriptpythondjangodjango-channels

django channels hadling connection


I get trouble with django channels i guess its with hadling connection on client, but maybe isnt. So my code look like this:

class ExternalDataConsumer(AsyncWebsocketConsumer):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.active_connections = {}

    async def connect(self):
        self.room_group_name = f"external_{self.scope['user'].id}"  # Match the group name from the signal
        await self.channel_layer.group_add(self.room_group_name, self.channel_name)
        logger.info(f"Connected to group: {self.room_group_name}")
        await self.accept()

    async def disconnect(self, close_code):
        await self.channel_layer.group_discard(self.room_group_name, self.channel_name)

    async def receive(self, text_data):
        try:
            data = json.loads(text_data["text_data"])
            if data:
               asyncio.ensure_future(await self.start_external_stream(data))
            else:
               asyncio.ensure_future(await self.stop_extenal_stream(data))
            # start or stop external stream

        except Exception as e:
            logger.error(f"ERROR receiving websocket: {e}")

   async def start_external_stream(self, data):
      # do something with data, send payload and get data from stream
   async def stop_external_stream(self, data):
      # stop existing steam for data
   async def send_notification(self, data):
      # sending notification to user

So issue is when i start getting data from external stream its double each time when user reload page. On client im just like get data from django channels websocket like this:

 if (isWebSocketConnected === 'true') {
    socket = new WebSocket('ws://0.0.0.0:8000/ws/binance_consumer/');
} else {
    socket = new WebSocket('ws://0.0.0.0:8000/ws/binance_consumer/');
    localStorage.setItem('websocketConnected', 'true');
}

const notifications = [];

// Handle WebSocket events
socket.addEventListener("open", (event) => {
    console.log("WebSocket connected");
});

socket.addEventListener("message", (event) => {
    const data = JSON.parse(event.data);
    // Extract the message and timestamp from the received data
    const message = data.message;
    const timestamp = data.timestamp;

    // Add the message to the notifications array along with the timestamp
    notifications.push({ message, timestamp });

    // If the number of notifications exceeds the maximum, remove the oldest ones
    if (notifications.length > maxNotifications) {
        notifications.shift(); // Remove the oldest notification
    }

    // Update the UI to display the notifications
    updateNotificationsUI();
});

What i can miss with this logic, so data for each user isnt doubled each time when his realod page?


Solution

  • so, i guess im fix this issue, just check if subscription exits like this

    async def connect(self):
        self.room_group_name = f"external_{self.scope['user'].id}"
        
        if self.room_group_name in self.active_connections:
            await self.close()
        else:
            self.active_connections[self.room_group_name] = self.channel_name
            await self.channel_layer.group_add(self.room_group_name, self.channel_name)
            logger.info(f"Connected to group: {self.room_group_name}")
            await self.accept()