0
Comet web applications with Python,
                 Django & Orbited


@ PyCon Italia Qu4ttro       Massimo Scamarcia
htt...
Comet web applications with Python,
        Django & Orbited




How to push real-time data to
      web browsers?



    ...
Comet web applications with Python,
                  Django & Orbited


AJAX Polling
   Client: sends AJAX request.
   ...
Comet web applications with Python,
              Django & Orbited




Latency, bandwidth, memory usage and scaling issues...
Comet web applications with Python,
         Django & Orbited




      What about Comet?



              
Comet web applications with Python,
                Django & Orbited




Not a technology in itself
 Long Polling, IFRAME ...
Comet web applications with Python,
         Django & Orbited




          It's an hack!
       Still waiting for an unan...
Comet web applications with Python,
                      Django & Orbited


Long Polling
   Client: sends AJAX request.
...
Comet web applications with Python,
                        Django & Orbited

Http Push (HTTP Streaming*)
   Client: send...
Comet web applications with Python,
                    Django & Orbited
   HTTP Push: better for heavily-loaded apps.
  ...
Comet web applications with Python,
         Django & Orbited




      What about Orbited?



              
Comet web applications with Python,
        Django & Orbited




        He's here to solve problems!

                
Comet web applications with Python,
                     Django & Orbited


   Based on Twisted.
   STOMP* (ActiveMQ, Ra...
Comet web applications with Python,
           Django & Orbited




Using Django and Orbited
        Example app developed...
Comet web applications with Python,
                  Django & Orbited

Django is used for:
   Application logic.
   Tem...
Comet web applications with Python,
        Django & Orbited




              
Comet web applications with Python,
                    Django & Orbited

orbited.cfg                    settings.py
 [glo...
Comet web applications with Python,
                  Django & Orbited

Django base template (<head /> tag)
 <script>docum...
Comet web applications with Python,
                     Django & Orbited
<script>
$(document).ready(function() {
    stom...
Comet web applications with Python,
                   Django & Orbited

Django view
 @require_POST
 @login_required
 def ...
Comet web applications with Python,
        Django & Orbited




       Does it scale?



              
Comet web applications with Python,
        Django & Orbited




              
Comet web applications with Python,
        Django & Orbited




              
Comet web applications with Python,
         Django & Orbited




           Thank you
       and have fun with the quiz g...
Upcoming SlideShare
Loading in...5
×

Comet web applications with Python, Django & Orbited

1,528

Published on

Massimo Scamarcia

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,528
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Comet web applications with Python, Django & Orbited"

  1. 1. Comet web applications with Python, Django & Orbited @ PyCon Italia Qu4ttro Massimo Scamarcia http://www.pycon.it/ http://www.webright.it/    
  2. 2. Comet web applications with Python, Django & Orbited How to push real-time data to web browsers?    
  3. 3. Comet web applications with Python, Django & Orbited AJAX Polling  Client: sends AJAX request.  Server:  Data available: sends response.  No data: sends empty response.  Client: sends AJAX request again.    
  4. 4. Comet web applications with Python, Django & Orbited Latency, bandwidth, memory usage and scaling issues!    
  5. 5. Comet web applications with Python, Django & Orbited What about Comet?    
  6. 6. Comet web applications with Python, Django & Orbited Not a technology in itself Long Polling, IFRAME stream, HTMLFile, XHR Streaming, HTML5 SSE    
  7. 7. Comet web applications with Python, Django & Orbited It's an hack! Still waiting for an unanimous way to do it.    
  8. 8. Comet web applications with Python, Django & Orbited Long Polling  Client: sends AJAX request.  Server: holds the request.  Data available: sends response.  Timeout reached: empty response.  Client: sends AJAX request again.    
  9. 9. Comet web applications with Python, Django & Orbited Http Push (HTTP Streaming*)  Client: sends HTTP request.  Server: doesn't terminate the connection.  Server: sends data when available.  Connection closed:  Data is queued.  Client reconnects. [*] http://ajaxpatterns.org/HTTP_Streaming    
  10. 10. Comet web applications with Python, Django & Orbited  HTTP Push: better for heavily-loaded apps.  Not cross-browser.  Long Polling: cross-browser and easy.  Bandwidth usage.  Too many connections.  Async Python servers* are better for both:  Twisted, Tornado, Dieselweb, Eventlet, Concurrence, Circuits, Gevent, Cogen. [*] http://nichol.as/asynchronous-servers-in-python    
  11. 11. Comet web applications with Python, Django & Orbited What about Orbited?    
  12. 12. Comet web applications with Python, Django & Orbited He's here to solve problems!    
  13. 13. Comet web applications with Python, Django & Orbited  Based on Twisted.  STOMP* (ActiveMQ, RabbitMQ), IRC and XMPP protocol support.  Ready-to-use and cross-browser JavaScript client code.  You don't have to reinvent the wheel! [*] http://stomp.codehaus.org/    
  14. 14. Comet web applications with Python, Django & Orbited Using Django and Orbited Example app developed for PyConIt4: http://pyconquiz.webright.it/    
  15. 15. Comet web applications with Python, Django & Orbited Django is used for:  Application logic.  Template rendering.  User auth and registration.  Handling AJAX requests.  Sending messages to STOMP server. ...Orbited and jQuery do the rest!    
  16. 16. Comet web applications with Python, Django & Orbited    
  17. 17. Comet web applications with Python, Django & Orbited orbited.cfg settings.py [global] # Available using context #reactor=select # processor or templatetag #reactor=kqueue reactor = epoll ORBITED_HOST = 'localhost' proxy.enabled = 1 ORBITED_PORT = 8080 session.ping_interval = 300 STOMP_HOST = 'localhost' [listen] STOMP_PORT = 61613 http://localhost:8080 stomp://localhost:61613 [access] * -> localhost:61613    
  18. 18. Comet web applications with Python, Django & Orbited Django base template (<head /> tag) <script>document.domain = document.domain;</script> <script src="{{ ORBITED_MEDIA_URL }}Orbited.js"></script> <script type="text/javascript"> Orbited.settings.port = {{ ORBITED_PORT }}; Orbited.settings.hostname = "{{ ORBITED_HOST }}"; Orbited.settings.streaming = true; TCPSocket = Orbited.TCPSocket; </script> <script src="{{ ORBITED_MEDIA_URL }}JSON.js"></script> <script src="{{ ORBITED_MEDIA_URL }}protocols/stomp/stomp.js"></script>    
  19. 19. 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("Error: " + error);}; stomp.onerrorframe = function(frame){ debug("Error: " + 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>    
  20. 20. 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({})    
  21. 21. Comet web applications with Python, Django & Orbited Does it scale?    
  22. 22. Comet web applications with Python, Django & Orbited    
  23. 23. Comet web applications with Python, Django & Orbited    
  24. 24. Comet web applications with Python, Django & Orbited Thank you and have fun with the quiz game!    
  1. A particular slide catching your eye?

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

×