Django and Long Polling
Asked Answered
M

2

19

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!

Meingolda answered 22/3, 2014 at 19:26 Comment(7)
It's not clear what you are asking for... At least not to meInflect
@JoranBeasley stackoverflow.com/questions/tagged/long-pollingMasseter
https://mcmap.net/q/640773/-does-django-have-a-way-to-open-a-http-long-poll-connectionGenerable
also: github.com/tbarbugli/django_longpollingGenerable
@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.Meingolda
Can you please provide a code sample of what you're trying to achieve? There are a 100 different ways to approach this. Thanks.Cristicristian
I think server sent events could be used here: You can refer to github.com/fanout/django-eventstreamStitch
B
1

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.

Brueghel answered 30/3, 2014 at 13:40 Comment(3)
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 pixeldonor.com/2014/jan/10/django-gevent-and-socketio a possible (good) solution?Wimberly
I'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: #17515133Brueghel
Your link point to a project where the last commit goes back to 7 months. Doesn't look very much active.Wimberly
P
0

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/

Platina answered 3/3, 2023 at 0:45 Comment(1)
I don't think this has anything to do with the question. if so, please explain.Octagon

© 2022 - 2024 — McMap. All rights reserved.