Reputation: 147
What is the best way to update a UI element depending on a change in the database? For example, whenever someone comments on a post, Facebook automatically updates the element for each user - how is this done?
I know that data pulling is one way to do it but are there any better procedures?
I would like to know how something like this can be done with Python (Django) but any other generic solution is welcome as well.
Upvotes: 3
Views: 2459
Reputation: 1479
What you're looking for is the websockets protocol:
WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection
and usually the default way to do with Django is to use the django-channels project:
Channels augments Django to bring WebSocket, long-poll HTTP, task offloading and other async support to your code, using familiar Django design patterns and a flexible underlying framework that lets you not only customize behaviours but also write support for your own protocols and needs.
You will probably need to spend some time to configure the channels
setup and modify your application to use it, but if you're looking to "push" data from backend to the frontend after a certain action has finished, this is probably the way to do it.
I would recommend looking into the channels-examples repository for an example chat implementation which uses django-channels
.
Upvotes: 1