i have a database that store some rows that are generated every second. I would like to update website every new content arrives to db, and i don't want to refresh page, but add new rows to current page. What is the best approach to this?
Asked
Active
Viewed 2,350 times
1
-
You need to implement comet, and the best way is use socket.io. Then in your code you will probably be implementing a generator and returns result as it gets it. See Django-socketio: http://pypi.python.org/pypi/django-socketio/ This is not a trivial fix. The short easy solution is just use short-polling (use ajax and asks for new content every 5-6 seconds, let say). That solution is very inefficient, but requires little hack. Mostly on the front-end. The other alternative method in comet is long polling, but it still requires quite a lot of changes. So I'd invest ur time in socket.io – User007 Aug 14 '12 at 21:22
1 Answers
2
You should use AJAX queries, either of two techniques:
- Periodically have the page request updates (and update the table with the result in JavaScript; backend remains plain django); or
- Use long-polling, a technique commonly known as "comet", for keeping a connection open with the server and receiving a server event when there is an update. The backend for this could be a bit tricky in frameworks designed for request/response pattern; but you can find leads on how to do it in python here.
-
Will AJAX and getting data via it (probably in JSON format with jQiery getJSON function) not kill site's performance if I want to pool data every 1. sec? – Aug 14 '12 at 21:53
-
That really depends on your app, number of users, load patterns etc. In common web app scaling, updating every 1 second will be considered a bit too aggressive. But if you're not currently having too much load, it will save you the trouble of implementing comet over django. If you are flexible enough to limit your app to HTML5 clients only, you can use websockets - which has [some support in django](http://pypi.python.org/pypi/django-websocket/0.3.0). – onon15 Aug 15 '12 at 04:55