vendredi 4 janvier 2019

django Web-Service design and in-memory data storage

Objective: to provide end-user number of 'notifications' in (almost) real time.

enter image description here

To keep it simple, notifications should come when userX submits form XYZ and all other users should see that the number of notifications incremented by 1 (if userY see the number 50 that means there are 50 NEW XYZ forms).

Question #1: given my django channels web-service, where should I iterate to get the result? At the moment I placed it under websocket_connect with an endless loop such that:

class EchoDiscussionNotificationConsumer(AsyncConsumer):

async def websocket_connect(self, event):

    await self.send({
        "type": "websocket.accept",
    })

    # NOT SURE THIS IS A GOOD DESIGN! 
    while True:
        await asyncio.sleep(2)
        rand = random.randint(1,100)
        mesg = "#"+str(rand)

        await self.send({
            'type': 'websocket.send',
            'text':  mesg,
        })

This works great but I don't think this is a good design.

Question #2: I don't want to query the db every 2 seconds what I had in mind is to query only when (1) the user logs in and (2) another user submits form XYZ. So once I have a 'table of notifications' from the db where should I store it (in-memory) to have faster access? (session?)

Aucun commentaire:

Enregistrer un commentaire