I need to implement long polling in my application to retrieve the events. But I have no idea how to do it. I know the concept of long polling, i.e to leave the connection open, until an event occurs. But how do I do implement this in my project. If you could give me a simple long polling example of client side and the views i guess, I would really appreciate. Thank you!
-
It's not clear what you are asking for... At least not to me – Joran Beasley Mar 22 '14 at 19:43
-
@JoranBeasley http://stackoverflow.com/questions/tagged/long-polling – frnhr Mar 22 '14 at 20:02
-
http://stackoverflow.com/a/4788034/2387772 – yuvi Mar 22 '14 at 20:04
-
also: https://github.com/tbarbugli/django_longpolling – yuvi Mar 22 '14 at 20:05
-
@JoranBeasley Hi! I have an app where users upload videos. So when someone uploads a video, I want to notify other users that a new video has been uploaded, like twitter or facebook or even SO. Also, I want to notify the users, if someone comments on their video. But I think I can achieve this, if I know how to implement long polling and notify the users about new video uploaded. So, all I need to know, is to implement long polling to notify users about new object being saved in the db. Hope I was clear. Please ask me if I wasn't. Thank you. – Robin Mar 22 '14 at 20:25
-
Can you please provide a code sample of what you're trying to achieve? There are a 100 different ways to approach this. Thanks. – joshlsullivan Feb 22 '23 at 20:34
-
I think server sent events could be used here: You can refer to https://github.com/fanout/django-eventstream – nofoobar Apr 04 '23 at 07:39
2 Answers
Disclaimer: this answer is long outdated. As of 2020, there is a ton of solutions for this problem, with django channels being only one of the options.
<< Disclaimer
very simple example:
import time
def long_polling_view(request):
for i in range(30): #e.g. reopen connection every 30 seconds
if something_happened():
...
return http.HttpResponse(
arbitrary_JSON_content,
mimetype='application/javascript'
)
time.sleep(1)
return http.HttpResponse({}, mimetype='application/javascript')
from the client side, you have to handle timeout and reopen connection.
However, I should say it's generally bad approach, by a number of reasons:
- it's computationally expensive both for client and server
- it's sensible to environment, e.g. timeouts
- it's still subject to 1 second delay (time.sleep() in example)
In most cases, checking for responses in setTimeout() every 3-5-10 seconds works just fine, and it's more efficient in terms of resources.
But there is a third option even better than that. Actually, long polling was more of a historical thing when there was nothing else to do to get realtime updates. Websockets are faster, inexpensive and now available in Django.

- 15,215
- 2
- 39
- 48
-
your link to websocket solution is broken. I implemented something similar with Django from client side using ajax. But still the problem with timeouts. Is gevent http://www.pixeldonor.com/2014/jan/10/django-gevent-and-socketio/ a possible (good) solution? – Pietro Aug 19 '14 at 18:58
-
1I've fixed link. Is gevent a good solution? It depends. You should have a good understanding of how greenlets work, otherwise you can run into really strange issues: http://stackoverflow.com/questions/17514633/gevent-and-posgres-asynchronous-connection-failed/18956319 – Marat Aug 19 '14 at 23:16
-
1Your link point to a project where the last commit goes back to 7 months. Doesn't look very much active. – Pietro Aug 20 '14 at 05:22
you can use celery with django. django provide elementary asynchronous support which is not fully complete yet and face performance issues. Celery could be a good solution for your problem.
you can follow this tutorial for basic understanding
https://realpython.com/asynchronous-tasks-with-django-and-celery/

- 769
- 10
- 22
-
I don't think this has anything to do with the question. if so, please explain. – Kassab Jun 08 '23 at 18:30