Comet with Django and APE

Recently I’ve been looking at using the AJAX Push Engine (APE) from Weeyla for adding Comet functionality to a Django site. To get a feel for for how APE works, I threw together a really trivial message posting app, and figured I’d document my process for those who might also be interested in integrating APE with Django. Note: This post assumes you know how to set up a Django project, and know enough about Django to realize this code is missing lots of really important bits (validation, authentication, etc…). This is going to be a short novel of a blog post, with a lot of code, so bear with me…

So, to start with, I just slapped together a really basic message posting app. Just a message with a name and a timestamp.

And here’s the template:

Ok, so that’s the basic setup… Pretty boring, right? If Sue submits a message, Bob has to refresh his page to see it. So, we’re going to add APE into the mix to update Bob right away. Getting APE up running it pretty easy, just follow the instructions over at the APE Wiki. We’ll be using APE’s inlinepush server module. Just for good measure we’ll be tossing some jQuery in as well.

So, the basic idea is we need to connect to a APE “channel” which we’ll use to receive updated messages. We’ll use jQuery to send the messages. So let’s start by using jQuery to submit the form, with the following script:

And here are the updated views:

And so with that, we can now submit messages without reloading the page. Any other users, however, would need to refresh to see the new messages. That’s where APE comes in. Here’s the APE client code (along with the updated append_message function):

And here’s the updated view to send new posts to APE:

You’ll notice I’ve added two settings to

APE_PASSWORD = 'testpasswd'
APE_SERVER = 'http://ape-test.local:6969/?'

And that’s all there is to it! Like I said earlier, this is a very incomplete application, and a lot of the “boilerplate” work is left as an exercise for the reader. It should be more than enough to get you on your way though. Perhaps in a later post I’ll move the APE update from inside the view to a post_save signal on the model itself. That’s for another time though…

UPDATE: Well, another time is today. Check out this post about adding a signal handler.

8 thoughts on “Comet with Django and APE”

  1. Hi, the post is quite useful, but I have a question.
    How are the client (js) and the APE server connected?
    As far as I know, APE client injects iframe into the page. Bud where does it get the APE server address?
    Another question – the APE server be configured to be accessible from outside?

  2. Hey, I hate to be “that guy” but I am reading this on my iPod and the code snippets do not get fully rendered. You may be able to replicate by reloading the page with a really narrow window size. I can’t scroll to read them and turning the thing sideways doesn’t help.

    Anyway thanks for the tutorial, I’ll read it on my laptop when I get home, looks like just what I need right now!

    1. The code snippets are GitHub “gists”. I don’t have any control over the styling of the gists, it’s just an embed code. You may have an easier time viewing them by hitting the “This Gist” link that accompanies each one and viewing it directly on GitHub.

  3. Hello, i have been testing this example for some time now… a bit confusing, but at the end i got it working :).. for those wondering about the views urls, you must include in your urls something like this (url path depends on your project)

    (r’^ape-jsf/(?P.*)$’, ‘django.views.static.serve’,{‘document_root’: ‘ape-jsf’}),
    (r’^ape-jsfindex.html/(?P.*)$’, ‘django.views.static.serve’,{‘document_root’: ‘ape-jsf’}),

    this urls are to allow APE post methods.

    And these are the extra imports needed for the views:
    import urllib2
    import django.utils.simplejson as json

    if you are having problems with APE_SERVER url, you can try:

  4. Hi,
    thanks for that post. I’m making site with Django and I need to implement real time chat.
    The solution You described – is it a proper solution of using comet (APE) with Django or there are different ways to do it?
    I need to check some things before sending message (if authenticated, permissions etc) in Django view.
    As I read on APE site, they make sending also via Comet, not to server, sso that’s from my question about any different methods?

    Thanks in advance.

  5. Great article, thank you! Especially the last bit of code helped me alot. Was quite a fiddle to bring that ape up and running though.

    I hope that real-time web applications will become standard – sooner or later. I feels very responsive and gives instant feedback of all user actions. In addition to the Semantic Web, this is what i think about when people talk about Web 3.0.

Leave a Reply

Your email address will not be published. Required fields are marked *