I am trying to design an application where users can make posts with Django on the backend. Right now I have to refresh the page with JavaScript every 20 or 30 seconds to check for new posts that might be available for that page. I was wondering if there are more effective ways to do this? Maybe with a JSON response? I am looking for a solution like Twitter, where they show how many new posts are available that are not on the page or even like on Facebook where they update the wall posts automatically.

Is there a particular technology should I be researching in order make the updates more efficient?

  • 2
    StackExchange pages don't refresh themselves automatically, but they do get notifications when they have new content available, so the user can choose to display it. Have a look at the JS involved and see if you can't pick up some ideas... :) – Mason Wheeler Feb 5 '13 at 19:11
  • I believe what you're looking for is ibm.com/developerworks/library/wa-cometjava Comet or long polling technique. – toy Feb 5 '13 at 19:18
  • If you're using Python stream-hub.com – toy Feb 5 '13 at 19:18
  • @MasonWheeler Yes that's exactly what I am looking for. I would defiantly have a look into it. So is using js for this the right approach or is there a better and easier solution? – Jonathan Feb 5 '13 at 19:18
  • @toy looking into it now – Jonathan Feb 5 '13 at 19:20

There are several methods for this:

  1. Web Sockets
  2. Long Polling (or the blanket term Comet)

For solutions, check out:

  1. django-socketio
  2. Not a django implementation, but a great resource for concepts: SignalR

I would recommend going the websockets route, then falling back to long polling. Here's a fairly scholarly article on long polling and best practices.

  • Except "because it's an old browser which doesn't support it", are there other situations where web sockets aren't available (and so you need to have implemented the fallback)? – ChrisW Apr 25 '19 at 21:55
  • @ChrisW Yes, there are several reasons beyond browser support: if your application has a reverse proxy in front of it that doesn't support or isn't configured to handle the "upgrade" header (necessary to establish a websocket connection), if you're running your site/REST services in a FaaS scenario (AWS API Gateway now supports websockets, but other cloud providers like Azure don't support it) – Zachary Yates Apr 26 '19 at 2:51
  • Although, since it's now 2019 there's far less reason to do the extra work to fall back to long-polling. – Zachary Yates Apr 26 '19 at 2:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.