Saturday 26 December 2015

SwampDragon push notification

SwampDragon push notification
Push notification, one of the complex problem that a occurs for a web developer. Old time we need to install number of packages and make numerous changes in django files especially wsgi.py file to make it work. But now we have SwampDragon to help us out. SwampDragon made push notification too easy (not that easy...) implement compared to former method.

SwampDragon

SwampDragon helps to create near real-time functionality for Django via web socket support.Swamp dragon consists of:
  • Redis. Basically a very quick, persistent kind of memcache. Supports pubsub.
  • Tornado. A python webserver. Non-blocking IO. Lots of connections are no problem.
  • Django.
Below I shown the simple work flow using swampdragon and django:
 
How To 
 
Below I have explained how to send notification to one or more users using same channel with the help of swamdragon and django.
Hope that you have already created your django app.
After that install the following packages:
To install SwampDragon:
 
pip install swampdragon
or

pip install -e git://github.com/jonashagstedt/swampdragon.git#egg=swampdragon
Install redis server (If you don't have redis)
 
sudo apt-get install redis-server
After installation redis server will start automatically. If not use the following command
 
sudo service redis-server start
Now redis server will be up and running. In settings.py file add swampdragon to installed app.
Also add SWAMP_DRAGON_CONNECTION and DRAGON_URL.
 
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'swampdragon',
) 
SWAMP_DRAGON_CONNECTION = ('swampdragon.connections.sockjs_connection.DjangoSubscriberConnection', '/data')

DRAGON_URL = 'http://localhost:9999/'

Create router.py file in the app folder. Router.py helps in routing incoming connection to specified endpoint. 
 
from swampdragon import route_handler
from swampdragon.route_handler import BaseRouter

class MyRouter(BaseRouter):
    route_name = 'my-route'
    valid_verbs = [ 'subscribe']

    def get_subscription_channels(self, **kwargs):
        return ['notification']

route_handler.register(MyRouter)

Now create a js file. Let it be router.js .
Add the channel subscription and all display method for incoming message, inside the router.js file.
 
swampdragon.ready(function () {
//subscribes the channel and router
    swampdragon.subscribe('my-route', 'notification', null, function (context, data) {
        // any thing that happens after successfully subscribing
    }, function (context, data) {
        // any thing that happens if subscribing failed
    });
});
//Extracts message in the channel
swampdragon.onChannelMessage(function (channel, data) {
            InsertMsg2Html( data.data.message);
});

//inserts the received message from the channel to html 
function InsertMsg2Html(msg) {
    var messages = document.getElementById("notification");

    var span = document.createElement("span");
    messages.insertBefore(span, notification.firstChild);
    div.innerHTML = "<strong>" + msg + "</strong> " ;
}
In your template add the following lines:

    <script type="text/javascript" src="http://localhost:9999/settings.js"></script>
    <script type="text/javascript" src="js/sockjs-0.3.4.min.js"></script>
    <script type="text/javascript" src="js/swampdragon.js"></script>
    <script type="text/javascript" src="/js/router.js"></script>
    <script type="text/javascript" src="js/datamapper.js"></script>
    <script type="text/javascript" src="js/services.js"></script>
Now in views.py 

from swampdragon.pubsub_providers.data_publisher import publish_data

data = { 'message': 'You have successfully send a request'}
publish_data(channel='notification', data=data)
Here publish_data method will publish the data to the specified channel. onChannelMessage mentioned in the router.js gets the published message. Remaining logic to what to do with the incoming message can be changed depending up on the user requirement.

No comments:

Post a Comment