3. 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)
Avinash Prasad 09/29/2012
4. What next ?
●
Lets bring some life to our web application
● Make it faster
● Enhance the user experience
But before that....
Avinash Prasad 09/29/2012
5. Techniques used to make Rich Internet
Applications
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.
Avinash Prasad 09/29/2012
6. Techniques used to make Rich Internet
Applications
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.
Avinash Prasad 09/29/2012
7. Techniques used to make Rich Internet
Applications
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.
Avinash Prasad 09/29/2012
8. Lets bring in some awesomeness.....
Web Sockets
The WebSocket Interface defines a full duplex communications channel that is
exposed via a JavaScript interface in HTML 5 compliant browsers.
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
Avinash Prasad 09/29/2012
9. 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
};
Avinash Prasad 09/29/2012
10. 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.
Avinash Prasad 09/29/2012
11. 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
Avinash Prasad 09/29/2012
12. Django-socketio
Components
● Channels
● Broadcast mechanism
● Events
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:
Avinash Prasad 09/29/2012
13. 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)
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.
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.
Avinash Prasad 09/29/2012