Real time web_apps_pycon2012-v1

15
Real Time Django Applications Avinash Prasad Python developer Xoriant Solutions

description

Alpha version of the presentation

Transcript of Real time web_apps_pycon2012-v1

Page 1: Real time web_apps_pycon2012-v1

Real Time Django Applications

Avinash PrasadPython developer

Xoriant Solutions

Page 2: Real time web_apps_pycon2012-v1

& =Great Web Apps

09/29/2012Avinash Prasad

Page 3: Real time web_apps_pycon2012-v1

A real time application:

● Accesses real-time near real-time data

● Connectivity via full duplex network connections

● Capable of pushing data to the client at anytime

A web application:

● Started as a static content delivery mechanism over a stateless request and response architecture of HTTP.

● Emergence of technologies like AJAX, Servlets brought a revelation in the web application development making it dynamic(aka RIA)

09/29/2012Avinash Prasad

Page 4: Real time web_apps_pycon2012-v1

What next ?

09/29/2012Avinash Prasad

● Lets bring some life to our web application

● Make it faster

● Enhance the user experience

But before that....

Avinash PrasadAvinash Prasad

Page 5: Real time web_apps_pycon2012-v1

Techniques used to make Rich Internet Applications

09/29/2012Avinash PrasadAvinash PrasadAvinash Prasad

Polling● Easiest way to retrieve the latest data and events from the server for a Web application.

● The Web application essentially polls the server on a regular basis, based on a timer.

● The timeliness of the data is directly proportional to the polling frequency.

● Uses a simple HTTP request each time the timer expires.

Page 6: Real time web_apps_pycon2012-v1

Techniques used to make Rich Internet Applications

09/29/2012Avinash PrasadAvinash PrasadAvinash Prasad

Asynchronous Polling● In asynchronous polling the browser sends a request to the server and the server keeps the request open for a set period.

● If a notification is received within that period, a response containing the message is sent to the client.

● If a notification is not received within the set time period, the server sends a response to terminate the open request and the browser resends the request to the server.

Page 7: Real time web_apps_pycon2012-v1

Techniques used to make Rich Internet Applications

09/29/2012Avinash PrasadAvinash PrasadAvinash Prasad

Streaming● When streaming is used, the browser sends a complete request, but the server sends and maintains an open response that is continuously updated.

● A request is sent and kept open indefinitely (or for a set period of time) and the response is updated whenever a message is ready to be sent, but the server never signals to complete the response

● Thereby keeping the connection open to deliver future messages.

Page 8: Real time web_apps_pycon2012-v1

Lets bring in some awesomeness.....

09/29/2012Avinash PrasadAvinash PrasadAvinash Prasad

The WebSocket Interface defines a full duplex communications channel that is exposed via a JavaScript interface in HTML 5 compliant browsers.

Web Sockets

What web sockets can do for you ?

● Persistent connection between the client and the server enabling both parties to send data at any time.

● Reduces the overhead of HTTP, thereby making them well suited for low latency applications

Page 9: Real time web_apps_pycon2012-v1

09/29/2012Avinash PrasadAvinash PrasadAvinash Prasad

Web Sockets

A web socket can be opened by calling the WebSocket constructor:

var connection = new WebSocket('ws://localhost:8005/chat', [subprotocols..]);

ws: new URL schema for WebSocket connections. Also an option for wss exists, socket over HTTPS

Communicating with the server// When the connection is open, send some data to the server

connection.onopen = function () {

connection.send('Ping'); // Send the message 'Ping' to the server

connection.onerror = function (error) {

console.log('WebSocket Error ' + error); // Log errors

};

connection.onmessage = function (e) {

console.log('Server: ' + e.data); // Log messages from the server

};

Page 10: Real time web_apps_pycon2012-v1

django to the rescue

Django can be used as the sever side component along with HTML5 WebSockets.An implementation called django-socketIO can be particularly used to build real time applications.

Django-socketio is a BSD licensed django application that brings together a variety of features that allow you to use websockets seamlessly with any django project.

Built after taking inspiration from applications built using Socket.IO and gevent with django.

09/29/2012Avinash PrasadAvinash PrasadAvinash Prasad

Page 11: Real time web_apps_pycon2012-v1

Django-socketio

Features

● A management command for running gevent's pywsgi server with auto-reloading capabilities

● A channel subscription and broadcast system that extends Socket.IO allowing WebSockets and events to be partitioned into separate concerns

● A signals-like event system that abstracts away the various stages of a Socket.IO request

09/29/2012Avinash PrasadAvinash PrasadAvinash Prasad

Page 12: Real time web_apps_pycon2012-v1

Django-socketio

Components

● Channels

● Broadcast mechanism

● Events

09/29/2012Avinash PrasadAvinash PrasadAvinash Prasad

Channels: A common requirement in websocket based communication are multiple channels so that communication can be divided effectively .django-socketio extends Socket.IO both on the client and server to provide channels that can be subscribed and broadcast to.

At JavaScript end we can use,

var socket = new io.Socket();socket.connect();socket.on('connect', function() { socket.subscribe('my channel');});Once the socket is subscribed to a channel, broadcast to the channel server-side is done at Python using the socket.broadcast_channel method:

Page 13: Real time web_apps_pycon2012-v1

09/29/2012Avinash PrasadAvinash PrasadAvinash Prasad

Broadcast Mechanism: Each server-side socket has the following methods,

● django_socketio.broadcast(message)

● django_socketio.broadcast_channel(message, channel)

● django_socketio.send(session_id, message)

Events: The django_socketio.events module is quiet similar to Django's signaling module and has events similar to Django's signals.

They are raised at any stage during a websocket request.

Each of the event enables us to implement our own socket handling logic using its session id.

Note:In send method, the socket is identified by its session ID, accessible via socket.session.session_id. Its web-socket's session id and not Django's session id.

Page 14: Real time web_apps_pycon2012-v1

Demo application(s) with brief description on implementation details

Page 15: Real time web_apps_pycon2012-v1

Thank You!