Bitbucket is a code hosting site with unlimited public and private repositories. We're also free for small teams!

Close

django push 2

A Django application for simulate pushing data from server to client, avoid the client to ask avery time if something changed, overloading the server.

Currently, only JSON data are available, so only text is send throught the channel.

N.B. Tested on Django==1.3

How does it work?

The Channels are created by admin interface. When you create them, you can also choose a time to live about the message arrived on that channel.

Channels are a simply queue of messages that have been sended from clients and forworded for others clients are listening that current channel/s.

So, both the clients and the administrator can create the channel, and one or more other clients can connecting on it.

When some message are sended by some user on the channel, all client connected to it, receive the message without ask to the server if something change every X time.

Where?

Tipical application is a chat. In general, in a chat, each clients ask every X second to server if it has a new message for them. Very bad for the server!!!

Instead, in this way, you open a connection with the server and only when you have a new mesage, it will send to you.

Installation

To install django-push2 from pip, just type:

pip install django-push2

It will install push2 and Django 1.3 if you haven't.

If you want to use virtaulenv, simply type:

$ virtualenv push2_env
$ source  push2_env/bin/activate
$ pip install django_push2

Configuration

Put in INSTALLED_APPS the application:

'push2',

and in urls.py insert:

"url(r'^push2/', include('push2.urls')),"

Thats it.

I tested it on postgres and sqlite and seems work well in both cases.

Remember that with this structure, you have to run (in my case) gunicorn with at least two workers, because in case you need to send a message on a channel and listen another one at the same time, two workers are necessary.

Demo

In demo folder I prepared two base templates that you can use to check how the application works. Put the demo folder in your project and configure it as show above.

N.B. Remember to declare STATIC_ROOT and MEDIA_ROOT in your settings.py

Now put in urls.py those rows:

url(r"^test/sender/$", direct_to_template, {"template": "sender.html"}),
url(r"^test/receiver/$", direct_to_template, {"template": "receiver.html"}),

url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
    'document_root': settings.STATIC_ROOT,
}, name='admin-media'),

The last one is to serve static file, the first two are the dispatcher for templates file that rapresent my demo.

Now you are ready to check if all work. First go in your admin interface and create two users to make a complete test. After that you can create one channel.

Is important to make an admin login befor loading tempaltes test because all functions need that

Now we need two browsers to do a test, one for each users.

If you point first browser with user (for ex. admin1) to http://localhost:8000/test/sender/ you will see a simple form to send message.

Instead, if you go with second browser on http://localhost:8000/test/receiver/ with admin2 user, only a title compare...

Now try to send a message from sender, and you should see in real time compare into receiver window.

Check on firebug if you have some error.

Take a look to the code

JAVASCRIPT

As you can see the files are pretty simple.. We have (actually), two base javascript functions under class Push: connect and write.

receiver.html:

"Push.connect([1,2])" say to client to connect on channel 1 and 2 to receive the messages from that channels.

The function uiEffects override the original to allow you to make what you want with data when will arrive. Original function simply print a console.log. In this case we'll append the results in body part.

sender.html:

Push.write([1],'Hi, I'm writing on channel 1');

This functions write on channel 1 your message. In our case send what you wrote in a form. The function uiEffectsAfterWrite can be ovverrided here if you want make something with your data after send.

DJANGO

I provided also three signals to manage the data passing on the server. One on write operations, one during a first connection and the last one when it gets message form the channel.

These signals are needed because the queue for every channel is limited from time to live that you can choose for every channel.

So, after that time, the queue start to clean the old data, so you may want to provide a way to save it.

API

So, at the end we have two service on server that you can call:

/push2/channel/connections/
/push2/channel/write/

to listen or write on a channel.

In jsPush.js, I provide respectively two functions that call that services:

Push.connect([1,112,32...])
Push.write([2,12,23,44],'Mesaggio da mandare a tutti i canali appena scritti')

To user Push variable you have to include in your template jQuery and:

<script src="/static/push2/js/jsPush.js"></script>

Conlcusion

The applications is only at the beginning so for every suggestion, bug or other, contact me!

Recent activity

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.