I have a database where new data is being inserted every second. When the websockets connection is first established, I want to send all the data using django channels. Then, I want the new data data that goes every second into the database to be sent through the same websocket. I have the following consumers.py
class DataConsumer(AsyncConsumer):
async def websocket_connect(self, event):
print("connected", event)
await self.send({
"type": "websocket.accept"
})
obj = ... send the whole db
await self.send({
'type': 'websocket.send',
'text': obj
})
while True:
await asyncio.sleep(1)
obj = ... send only new records
await self.send({
'type': 'websocket.send',
'text': obj
})
async def websocket_receive(self, event):
print("receive", event)
async def websocket_disconnect(self, event):
print("disconnected", event)
Here Django Channels - constantly send data to client from server they mention that the loop will never exit if the user disconnects. They present a potential fix, but I don't understand it well. How could I fix it?