implementing real time web applications with django

19
Implementing real time web applications with Django Kristian Øllegaard 1 onsdag den 6. juni 12

Upload: kristian-houlberg-ollegaard

Post on 20-Aug-2015

11.723 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Implementing real time web applications with Django

Implementing real time web applications with Django

Kristian Øllegaard

1

onsdag den 6. juni 12

Page 2: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

About me

• Software Developer/System Administrator at Divio

• Django since 0.96

• Danish, lived in Zurich 1,5 year

2

onsdag den 6. juni 12

Page 3: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

Why real time?

• Better user experience

• More options in front end

• Make the web feel like native apps

• Showing live data.

• Collaboration is much easier.

3

onsdag den 6. juni 12

Page 4: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

Finding the right tool

• Criteria's

• Use websockets, but have fallbacks

• Good browser support (incl. old IE)

• Should be usable from python

• Does not require extensive changes in frontend

• “As fast as it can be”

4

onsdag den 6. juni 12

Page 5: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

The tools you want

Node.js + Socket.io

5

onsdag den 6. juni 12

Page 6: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

The tools you want

Node.js + Socket.io(well, we don’t want this, but socket.io needs it)

5

onsdag den 6. juni 12

Page 7: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

The tools you want

• Node.js

• Built on Chrome's JavaScript runtime

• Uses an event-driven, non-blocking I/O model

• Socket.io

• One interface for all transport methods (sockets, polling, etc.)

• Compatible with almost everything

6

onsdag den 6. juni 12

Page 8: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

Why not implement it in Python?

• Already active community

• Can be used from python without too much trouble

• Most people know very basic javascript

• More importantly, frontend engineers, knows javascript and can therefore contribute to the different browser-specific implementations.

7

onsdag den 6. juni 12

Page 9: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

Using redis for cross-language communication

• Support for many datatypes

• Can be used both as storage and as a queue

• Implemented in many different languages

• For the usage in this talk, any other queue could have been used as well.

8

onsdag den 6. juni 12

Page 10: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

Basic concept

• Something happens, the user must be notified in real time

• From e.g. django we insert the new value into the queue

• Node.js listens on the queue and emits any content directly to the browser via socket.io

• This is btw. very fast!

Redis

Django Node.js E.g. Celery

Browser

publish

subscribe

publish

subscribe

9

onsdag den 6. juni 12

Page 11: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

Sample node.js app

var io = require('socket.io').listen(8001);var redis = require('redis').createClient();

redis.psubscribe("socketio_*"); // Could be any pattern

io.sockets.on('connection', function (socket) {    redis.on('pmessage', function(pattern, channel, key){        socket.emit(channel, key);    });});

10

onsdag den 6. juni 12

Page 12: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

Sample HTML/JS

<script src="http://localhost:8001/socket.io/socket.io.js"></script><script>  var socket = io.connect('http://localhost:8001/');  socket.on('socketio_news', function (data) {    console.log(data);  });</script>

11

onsdag den 6. juni 12

Page 13: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

Sample usage from Python

import redisimport jsonredis_subscribe = redis.StrictRedis()redis_subscribe.publish("socketio_news", json.dumps({   'title': 'Djangocon 2012',}))

12

onsdag den 6. juni 12

Page 14: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

Short demo

13

onsdag den 6. juni 12

Page 15: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

Hosting socket.io

• Nginx does not support websockets!

• Needs its own app, if hosted on an application cloud (e.g. heroku)

• Recommended to expose the node server directly

• But hey, it’s node.js, it scales!

14

onsdag den 6. juni 12

Page 16: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

Can I use this today?

• Yes

• But, please don’t

15

onsdag den 6. juni 12

Page 17: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

Client Authentication

• Socket.io handles authentication from node -> client

• Currently no authentication between django and node.

• Could possibly be solved by storing your sessions in redis and checking them on connection.

16

onsdag den 6. juni 12

Page 18: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

Notes

• Concept should work with any language/framework

• E.g. communicating between ruby and python

17

onsdag den 6. juni 12

Page 19: Implementing real time web applications with Django

@oellegaardgithub.com/KristianOellegaard

Questions?

http://kristian.io@oellegaard

18

onsdag den 6. juni 12