Comet web applications with Python, Django & Orbited

5,549 views

Published on

My own talk @ PyCon Italia 4

Developing "realtime" web applications it has never been so easy thanks to Orbited, an implementation of the "Comet" development model written in Python and Javascript.

The first part of the talk will introduce the argument showing all sort of different techniques, highliting all merits and defects and prove how Orbited could be used successfully in this field.

In the second part it will be showed the implementation of an application developed with Django web framework, that could be tried directly by the public.

Published in: Technology
1 Comment
15 Likes
Statistics
Notes
  • do you have demo about this?
    please send it to me?
    thank you!
    my gmail: nhxyj111@gmail.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
5,549
On SlideShare
0
From Embeds
0
Number of Embeds
322
Actions
Shares
0
Downloads
105
Comments
1
Likes
15
Embeds 0
No embeds

No notes for slide

Comet web applications with Python, Django & Orbited

  1. 1. Comet web applications with Python, Django & Orbited @ PyCon Italia Qu4ttro http://www.pycon.it/ Massimo Scamarcia http://www.webright.it /
  2. 2. Comet web applications with Python, Django & Orbited <ul><li>How to push real-time data to web browsers? </li></ul>
  3. 3. Comet web applications with Python, Django & Orbited AJAX Polling <ul><li>Client: sends AJAX request.
  4. 4. Server: </li><ul><li>Data available: sends response.
  5. 5. No data: sends empty response. </li></ul><li>Client: sends AJAX request again. </li></ul>
  6. 6. Comet web applications with Python, Django & Orbited Latency, bandwidth, memory usage and scaling issues!
  7. 7. Comet web applications with Python, Django & Orbited <ul><li>What about Comet? </li></ul>
  8. 8. Comet web applications with Python, Django & Orbited <ul><li>Not a technology in itself
  9. 9. Long Polling, IFRAME stream, HTMLFile, XHR Streaming, HTML5 SSE </li></ul>
  10. 10. Comet web applications with Python, Django & Orbited <ul><li>It's an hack!
  11. 11. Still waiting for an unanimous way to do it. </li></ul>
  12. 12. Comet web applications with Python, Django & Orbited Long Polling <ul><li>Client: sends AJAX request.
  13. 13. Server: holds the request. </li><ul><li>Data available: sends response.
  14. 14. Timeout reached: empty response. </li></ul><li>Client: sends AJAX request again. </li></ul>
  15. 15. Comet web applications with Python, Django & Orbited Http Push (HTTP Streaming*) <ul><li>Client: sends HTTP request.
  16. 16. Server: doesn't terminate the connection.
  17. 17. Server: sends data when available.
  18. 18. Connection closed: </li><ul><li>Data is queued.
  19. 19. Client reconnects. </li></ul></ul>[*] http://ajaxpatterns.org/HTTP_Streaming
  20. 20. Comet web applications with Python, Django & Orbited <ul><li>HTTP Push: better for heavily-loaded apps. </li><ul><li>Not cross-browser. </li></ul><li>Long Polling: cross-browser and easy. </li><ul><li>Bandwidth usage.
  21. 21. Too many connections. </li></ul><li>Async Python servers* are better for both: </li><ul><li>Twisted, Tornado, Dieselweb, Eventlet, Concurrence, Circuits, Gevent, Cogen. </li></ul></ul>[*] http://nichol.as/asynchronous-servers-in-python
  22. 22. Comet web applications with Python, Django & Orbited <ul><li>What about Orbited? </li></ul>
  23. 23. Comet web applications with Python, Django & Orbited He's here to solve problems!
  24. 24. Comet web applications with Python, Django & Orbited <ul><li>Based on Twisted.
  25. 25. STOMP* (ActiveMQ, RabbitMQ), IRC and XMPP protocol support.
  26. 26. Ready-to-use and cross-browser JavaScript client code.
  27. 27. You don't have to reinvent the wheel! </li></ul>[*] http://stomp.codehaus.org/
  28. 28. Comet web applications with Python, Django & Orbited <ul><li>Using Django and Orbited Example app developed for PyConIt4: http://pyconquiz.webright.it/ </li></ul>
  29. 29. Comet web applications with Python, Django & Orbited Django is used for: <ul><li>Application logic.
  30. 30. Template rendering.
  31. 31. User auth and registration.
  32. 32. Handling AJAX requests.
  33. 33. Sending messages to STOMP server. </li></ul>...Orbited and jQuery do the rest!
  34. 34. Comet web applications with Python, Django & Orbited
  35. 35. Comet web applications with Python, Django & Orbited orbited.cfg [global] #reactor=select #reactor=kqueue reactor = epoll proxy.enabled = 1 session.ping_interval = 300 [listen] http://localhost:8080 stomp://localhost:61613 [access] * -> localhost:61613 settings.py # Available using context # processor or templatetag ORBITED_HOST = 'localhost' ORBITED_PORT = 8080 STOMP_HOST = 'localhost' STOMP_PORT = 61613
  36. 36. Comet web applications with Python, Django & Orbited Django base template (<head /> tag) <script>document.domain = document.domain;</script> <script src=&quot;{{ ORBITED_MEDIA_URL }}Orbited.js&quot;></script> <script type=&quot;text/javascript&quot;> Orbited.settings.port = {{ ORBITED_PORT }}; Orbited.settings.hostname = &quot;{{ ORBITED_HOST }}&quot;; Orbited.settings.streaming = true; TCPSocket = Orbited.TCPSocket; </script> <script src=&quot;{{ ORBITED_MEDIA_URL }} JSON.js&quot;></script> <script src=&quot;{{ ORBITED_MEDIA_URL }} protocols/stomp/stomp.js&quot;></script>
  37. 37. Comet web applications with Python, Django & Orbited <script> $(document).ready(function() { stomp = new STOMPClient(); stomp.onopen = function() { debug('Connected.');}; stomp.onclose = function(c) { debug(Lost Connection, Code: '+c);}; stomp.onerror = function(error){ debug(&quot;Error: &quot; + error);}; stomp.onerrorframe = function(frame){ debug(&quot;Error: &quot; + frame.body);}; stomp.onconnectedframe = function() { {% if game %} stomp.subscribe('/games/{{ game.id }}/status'); stomp.subscribe('/games/{{ game.id }}/players'); {% else %}// subscribe to other channels...{% endif %} }; stomp.onmessageframe = function(frame){ // frame.headers.destination for channel, frame.body for JSON data destpatterns.dispatch(frame); }; stomp.connect('{{ STOMP_HOST }}', {{ STOMP_PORT }}); }); </script>
  38. 38. Comet web applications with Python, Django & Orbited Django view @require_POST @login_required def game_start(request, game_id): game = get_object_or_404(Game, id=game_id) if request.user != game.author: return HttpResponseForbidden() try: # start() method set game start_time and send JSON # to ”/games/{{ game.id }}/status” using stomp.py game.start() except GameError, e: return JSONResponse({'error': str(e)}) return JSONResponse({})
  39. 39. Comet web applications with Python, Django & Orbited <ul><li>Does it scale? </li></ul>
  40. 40. Comet web applications with Python, Django & Orbited
  41. 41. Comet web applications with Python, Django & Orbited
  42. 42. Comet web applications with Python, Django & Orbited <ul><li>Thank you and have fun with the quiz game! </li></ul>

×