Your SlideShare is downloading. ×
Implementing real time web applications with Django
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Implementing real time web applications with Django

10,875
views

Published on

At djangocon 2012

At djangocon 2012


5 Comments
17 Likes
Statistics
Notes
No Downloads
Views
Total Views
10,875
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
76
Comments
5
Likes
17
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Implementing real time web applications with Django Kristian Øllegaard 1onsdag 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 2onsdag 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 3onsdag den 6. juni 12
  • 4. Finding the right tool • Criterias • 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 4onsdag den 6. juni 12
  • 5. The tools you want Node.js + Socket.io @oellegaard github.com/KristianOellegaard 5onsdag 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 5onsdag den 6. juni 12
  • 7. The tools you want • Node.js • Built on Chromes 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 6onsdag 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 7onsdag 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 8onsdag 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 9onsdag 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 10onsdag 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 11onsdag 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 12onsdag den 6. juni 12
  • 14. Short demo @oellegaard github.com/KristianOellegaard 13onsdag 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 14onsdag den 6. juni 12
  • 16. Can I use this today? • Yes • But, please don’t @oellegaard github.com/KristianOellegaard 15onsdag 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 16onsdag den 6. juni 12
  • 18. Notes • Concept should work with any language/framework • E.g. communicating between ruby and python @oellegaard github.com/KristianOellegaard 17onsdag den 6. juni 12
  • 19. Questions? http://kristian.io @oellegaard @oellegaard github.com/KristianOellegaard 18onsdag den 6. juni 12