views:

115

answers:

1

As described in http://stackoverflow.com/questions/1836861/how-to-update-a-django-page-without-a-page-reload, I send periodic XMLHTTPRequests from the browser to the server using JavaScript to get those pieces of the webpage that changes during the course of my application.

However, most of the time, nothing changes; the server replies with the same response and the browser updates the webpage with contents that's already been there.

Obviously, the server should only reply if there is new data.

A thorough web research came up with http://ajaxpatterns.org/Periodic_Refresh#Lace, but dcoumentation is terse and I'm struggling to implement the concept in my Django project. I have a hard time believing this has not been done before.

Does anyone know any further resources I could use as guideline ?

+2  A: 

The difficulty in answering is in not knowing what the server-side resources are that are being returned to the user.

I'll make up something which may serve as an example. Let's say you were developing an application that allowed you to monitor in real-time comments being made by users on your site. We can do several things to make this possible:

  1. The server keeps track of when comments were added (.created field)
  2. The API to get the latest comments requires us to specify how old of comments we want
  3. The view queries and returns only those that have been added since then

models.py

class Comment(models.Model):
    text = models.TextField()
    created = models.DateTimeField(default=datetime.now())

urls.py

url(r'^comments/latest/(?P<seconds_old>\d+)/$',get_latest_comments),

views.py

def get_latest_comments(request, seconds_old):
    """
    Returns comments that have been created since the last given number of seconds
    have elapsed.
    """

    # Query comments since the past X seconds
    comments_since = datetime.datetime.now() - datetime.timedelta(seconds=seconds_old)
    comments = Comments.objects.filter(created__gte=comments_since)

    # Return serialized data or whatever you're doing with it
    return HttpResponse(simplejson.dumps(comments),mimetype='application/json')

On the client-side you get the JSON, check if it has a value, if so enumerate the items, and add the new items to your <div> tag or whatever.


As you can see, the development of the API to return only recently updated items is going to vary based on what content the server is returning.

From your question it sounds like you want the server to manage identifying what is recently updated, not the client (which is a good strategy). In that case, what you need to do is define:

  1. How is the server going to keep track of changes (in my example that's done by the 'created' field)
  2. How is the client going to request those changes
  3. How is the server going to identify which changes have happened in order to return them to the client via API?
T. Stone
Thank you so much for taking the time and providing such a detailed answer! I think my problem was that I mostly understand the pieces, but couldn't see how it all comes together.My application is mostly working now, just a few details (about which I will probably ask for help in another question ;-)Thanks again! Great job!
ssc