Reputation: 335
I am writing a Django project that display data from mysql database,
database is constantly getting update, how can I constantly send data in view.py. I tried looking at many modules I came across celery,Tornado, Django channels but they are mostly written for chat applications, also I tried considering using Node.js with Django. But what is the real solution for a simple data update in Django ?
Right now I simply use this code in my html file that refresh the page and gets the last data but the problem is that it makes the website run very slow :
<script>
var myVar = setInterval(ReLoad , 2000); //refresh every 2 seconds
function ReLoad() {
$("#live").load(document.URL + " #live");
}
</script>
Upvotes: 3
Views: 3654
Reputation: 945
You might want to look into ajax (asynchronous javascript). For something to update on the page you typically need to refresh the page. If only a part of the page needs to be refreshed in the background u can use ajax. You can look into google's angular js and facebook's React js libraries. They developed these so u could get a notification without refreshing. I Know u can integrate it into django easily (not quite sure how tho).
Hopefully this can give you some clues as to where to go from here !
Upvotes: 1
Reputation: 315
While you can definitely use web socket (as Joran recommened), I would recommend push notification. You can check out GCM and this Django app:
https://github.com/jleclanche/django-push-notifications
One of the problem with Django is you need to jump hoops to use web sockets. If you are interested you can check out this app:
https://django-websocket-redis.readthedocs.io/en/latest/running.html
You can also consider to use NodeJS to particularly serve websocket, which I think would be a simpler solution that forcing Django to use Websocket.
I hope this helps!
Upvotes: 0