KEMBAR78
Comet web applications with Python, Django & Orbited | ODP
Comet web applications with Python, Django & Orbited @ PyCon Italia Qu4ttro http://www.pycon.it/ Massimo Scamarcia http://www.webright.it /
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.
Server: Data available: sends response.
No data: sends empty response. Client: sends AJAX request again.
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 stream, HTMLFile, XHR Streaming, HTML5 SSE
Comet web applications with Python, Django & Orbited It's an hack!
Still waiting for an unanimous way to do it.
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.
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
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
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.

Comet web applications with Python, Django & Orbited

  • 1.
    Comet web applicationswith Python, Django & Orbited @ PyCon Italia Qu4ttro http://www.pycon.it/ Massimo Scamarcia http://www.webright.it /
  • 2.
    Comet web applicationswith Python, Django & Orbited How to push real-time data to web browsers?
  • 3.
    Comet web applicationswith Python, Django & Orbited AJAX Polling Client: sends AJAX request.
  • 4.
    Server: Data available:sends response.
  • 5.
    No data: sendsempty response. Client: sends AJAX request again.
  • 6.
    Comet web applicationswith Python, Django & Orbited Latency, bandwidth, memory usage and scaling issues!
  • 7.
    Comet web applicationswith Python, Django & Orbited What about Comet?
  • 8.
    Comet web applicationswith Python, Django & Orbited Not a technology in itself
  • 9.
    Long Polling, IFRAMEstream, HTMLFile, XHR Streaming, HTML5 SSE
  • 10.
    Comet web applicationswith Python, Django & Orbited It's an hack!
  • 11.
    Still waiting foran unanimous way to do it.
  • 12.
    Comet web applicationswith Python, Django & Orbited Long Polling Client: sends AJAX request.
  • 13.
    Server: holds therequest. Data available: sends response.
  • 14.
    Timeout reached: emptyresponse. Client: sends AJAX request again.
  • 15.
    Comet web applicationswith Python, Django & Orbited Http Push (HTTP Streaming*) Client: sends HTTP request.
  • 16.
  • 17.
    Server: sends datawhen available.
  • 18.
    Connection closed: Data is queued.
  • 19.
    Client reconnects. [*]http://ajaxpatterns.org/HTTP_Streaming
  • 20.
    Comet web applicationswith Python, Django & Orbited HTTP Push: better for heavily-loaded apps. Not cross-browser. Long Polling: cross-browser and easy. Bandwidth usage.
  • 21.
    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
  • 22.
    Comet web applicationswith Python, Django & Orbited What about Orbited?
  • 23.
    Comet web applicationswith Python, Django & Orbited He's here to solve problems!
  • 24.
    Comet web applicationswith Python, Django & Orbited Based on Twisted.
  • 25.
    STOMP* (ActiveMQ, RabbitMQ),IRC and XMPP protocol support.
  • 26.
    Ready-to-use and cross-browserJavaScript client code.
  • 27.
    You don't haveto reinvent the wheel! [*] http://stomp.codehaus.org/
  • 28.
    Comet web applicationswith Python, Django & Orbited Using Django and Orbited Example app developed for PyConIt4: http://pyconquiz.webright.it/
  • 29.
    Comet web applicationswith Python, Django & Orbited Django is used for: Application logic.
  • 30.
  • 31.
    User auth andregistration.
  • 32.
  • 33.
    Sending messages toSTOMP server. ...Orbited and jQuery do the rest!
  • 34.
    Comet web applicationswith Python, Django & Orbited
  • 35.
    Comet web applicationswith 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.
    Comet web applicationswith 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.
    Comet web applicationswith 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.
    Comet web applicationswith 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.
    Comet web applicationswith Python, Django & Orbited Does it scale?
  • 40.
    Comet web applicationswith Python, Django & Orbited
  • 41.
    Comet web applicationswith Python, Django & Orbited
  • 42.
    Comet web applicationswith Python, Django & Orbited Thank you and have fun with the quiz game!