1

I am currently developing an app with Django and PostgreSQL. Often, new information is sent to database (sometimes one time a day, sometimes over 30000 times a day). I have an UI (vue.js) and I want to see the data from the database showing to the client in real time.

Currently I am using Django Channels. I implemented websocket and used psycopg2 library to listen to notifications from postgresql triggers. My consumer looks like this:

    class WSConsumer(WebsocketConsumer):
        def connect(self):
            self.accept()
            
            # the whole table is sent to the client after the connection
            self.send(json.dumps(ClientSerializer(Client.objects.all(), many=True).data)) 
    
            # connecting to database
            conn = psycopg2.connect(dbname='...', user='...') 
            conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
            curs = conn.cursor()
            curs.execute("LISTEN update_clients_table;")
    
            # then I listen to any new data in this table and send it to the client
            while True:
                if select.select([conn],[],[],5) == ([],[],[]):
                    print("Timeout")
                else:
                    conn.poll()
                    while conn.notifies:
                        notify = conn.notifies.pop(0)
                        print("Got NOTIFY:", notify.pid, notify.channel, notify.payload)
                        message = notify.payload
                        self.send(message)
    
        def disconnect(self):
            self.close()

This is an example of handling changes in 1 table. I have 20 tables in total, and around 100 clients that can connect to the websocket.

My question: is it a good way of building real-time app with Django and PostgreSQL? Is it simple, convenient, secure? Are there other ways to build such an app?

Thanks in advance!

artemonsh
  • 113
  • 3
  • 13
  • If the database table is being updated via the Django ORM, there are packages that provide the functionality you are implementing https://github.com/LostMoa/djangochannelsrestframework#subscribing-to-all-instances-of-a-model. How is your data being sent to the DB? – Iain Shelvington Mar 16 '22 at 13:31
  • @IainShelvington database tables are being updated with a separate python script that takes data from other sources. – artemonsh Mar 16 '22 at 13:48
  • @IainShelvington Do I get it right that django consumer can subscribe to all changes that I make with the database table? If so, can you please provide an example of how to implement it with my consumer – artemonsh Mar 16 '22 at 13:53
  • The package I linked works by listening to signals emitted by Django, it will only work if your script(s) make changes using Django and not any other way. Are you are willing to change your scripts to use the Django ORM? – Iain Shelvington Mar 16 '22 at 13:58
  • Yes, I can update data in tables with Django. Thank you for the response!!! – artemonsh Mar 16 '22 at 14:13

0 Answers0