Kristian Øllegaard presented an approach for implementing real-time web applications using Django. He discussed using Node.js with Socket.io for the real-time capabilities and Redis for cross-language communication between the Node.js and Django applications. He demonstrated publishing messages from Django to a Redis queue that the Node.js application subscribed to and then emitted to the browser via Socket.io, providing real-time updates. While the approach can be used today, he cautioned against doing so without further work on aspects like client authentication.
Boost PC performance: How more available memory can improve productivity
Implementing real time web applications with Django
1. Implementing real time web
applications with Django
Kristian Øllegaard
1
onsdag den 6. juni 12
2. About me
• Software Developer/System Administrator at Divio
• Django since 0.96
• Danish, lived in Zurich 1,5 year
@oellegaard
github.com/KristianOellegaard
2
onsdag den 6. juni 12
3. 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.
@oellegaard
github.com/KristianOellegaard
3
onsdag den 6. juni 12
4. 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”
@oellegaard
github.com/KristianOellegaard
4
onsdag den 6. juni 12
5. The tools you want
Node.js + Socket.io
@oellegaard
github.com/KristianOellegaard
5
onsdag den 6. juni 12
6. The tools you want
Node.js + Socket.io
(well, we don’t want this, but socket.io needs it)
@oellegaard
github.com/KristianOellegaard
5
onsdag den 6. juni 12
7. 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
@oellegaard
github.com/KristianOellegaard
6
onsdag den 6. juni 12
8. 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.
@oellegaard
github.com/KristianOellegaard
7
onsdag den 6. juni 12
9. 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.
@oellegaard
github.com/KristianOellegaard
8
onsdag den 6. juni 12
10. Basic concept
• Something happens, the user
Redis
must be notified in real time publish publish
•
subscribe
From e.g. django we insert the
E.g.
new value into the queue Django Node.js
Celery
• Node.js listens on the queue subscribe
and emits any content directly Browser
to the browser via socket.io
• This is btw. very fast!
@oellegaard
github.com/KristianOellegaard
9
onsdag den 6. juni 12
11. 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);
});
});
@oellegaard
github.com/KristianOellegaard
10
onsdag den 6. juni 12
12. 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>
@oellegaard
github.com/KristianOellegaard
11
onsdag den 6. juni 12
13. Sample usage from Python
import redis
import json
redis_subscribe = redis.StrictRedis()
redis_subscribe.publish("socketio_news", json.dumps({
'title': 'Djangocon 2012',
}))
@oellegaard
github.com/KristianOellegaard
12
onsdag den 6. juni 12
14. Short demo
@oellegaard
github.com/KristianOellegaard
13
onsdag den 6. juni 12
15. 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!
@oellegaard
github.com/KristianOellegaard
14
onsdag den 6. juni 12
16. Can I use this today?
• Yes
• But, please don’t
@oellegaard
github.com/KristianOellegaard
15
onsdag den 6. juni 12
17. 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.
@oellegaard
github.com/KristianOellegaard
16
onsdag den 6. juni 12
18. Notes
• Concept should work with any language/framework
• E.g. communicating between ruby and python
@oellegaard
github.com/KristianOellegaard
17
onsdag den 6. juni 12
19. Questions?
http://kristian.io
@oellegaard
@oellegaard
github.com/KristianOellegaard
18
onsdag den 6. juni 12