Implementing real time web                          applications with Django                                 Kristian Ølle...
About me                  •     Software Developer/System Administrator at Divio                  •     Django since 0.96 ...
Why real time?                  •     Better user experience                  •     More options in front end             ...
Finding the right tool                  •     Criterias                        •   Use websockets, but have fallbacks     ...
The tools you want                        Node.js + Socket.io                                                             ...
The tools you want                                 Node.js + Socket.io                        (well, we don’t want this, b...
The tools you want                  •     Node.js                        •   Built on Chromes JavaScript runtime          ...
Why not implement it in Python?                  •     Already active community                  •     Can be used from py...
Using redis for cross-language                                communication                  •     Support for many dataty...
Basic concept                  •     Something happens, the user                                                          ...
Sample node.js app                        var io = require(socket.io).listen(8001);                        var redis = req...
Sample HTML/JS                        <script src="http://localhost:8001/socket.io/socket.io.js"></script>                ...
Sample usage from Python                        import redis                        import json                        red...
Short demo                                                      @oellegaard                                     github.com...
Hosting socket.io                  •     Nginx does not support websockets!                  •     Needs its own app, if h...
Can I use this today?                  •     Yes                  •     But, please don’t                                 ...
Client Authentication                  •     Socket.io handles authentication from node -> client                  •     C...
Notes                  •     Concept should work with any language/framework                        •   E.g. communicating...
Questions?                        http://kristian.io                         @oellegaard                                  ...
Upcoming SlideShare
Loading in...5
×

Implementing real time web applications with Django

11,763

Published on

At djangocon 2012

5 Comments
18 Likes
Statistics
Notes
No Downloads
Views
Total Views
11,763
On Slideshare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
82
Comments
5
Likes
18
Embeds 0
No embeds

No notes for slide

Transcript of "Implementing real time web applications with Django"

  1. 1. Implementing real time web applications with Django Kristian Øllegaard 1onsdag den 6. juni 12
  2. 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. 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. 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. 5. The tools you want Node.js + Socket.io @oellegaard github.com/KristianOellegaard 5onsdag den 6. juni 12
  6. 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. 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. 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. 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. 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. 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. 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. 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. 14. Short demo @oellegaard github.com/KristianOellegaard 13onsdag den 6. juni 12
  15. 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. 16. Can I use this today? • Yes • But, please don’t @oellegaard github.com/KristianOellegaard 15onsdag den 6. juni 12
  17. 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. 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. 19. Questions? http://kristian.io @oellegaard @oellegaard github.com/KristianOellegaard 18onsdag den 6. juni 12
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×