Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Real Time Web sans Node

Real Time Web sans Node

High traffic websites — Gmail, Facebook, Twitter — send live updates to their users all day long (think: collaborative editing in Google docs). Unfortunately, most of the sites we write wait for users to request data. Let’s look at what technologies exist in the Python world that can help us send data to our users.

Example code here:
https://github.com/wowzer/rtw

Michael Irani

March 18, 2014
Tweet

Other Decks in Technology

Transcript

  1. •  Blogs •  Hacker News •  News sites •  Flickr

    •  Craigslist •  Wikipedia •  Pandora •  Ebay HTTP – BUILT FOR REQUESTS
  2. ¡ Social feeds ¡ Site usage ¡ Sports scores ¡ Stock ticker ¡ Collaborative editing

    ¡ Chat ¡ Multiplayer games ¡ Server/process data BIDERECTIONAL COMMUNICATION
  3. Python @app.route('/websocket') def handle_websocket(): ws = request.environ.get('wsgi.websocket') if not ws:

    abort(400) while True: message = ws.receive() ws.send(“send message to client”) @app.route('/') def main(): root = 'templates', mimetype="text/html" return static_file('index.html', root=root) wsgi_server = WSGIServer(app, handler_class=WebSocketHandler) wsgi_server.serve_forever() Javascript var ws = new WebSocket("ws:// localhost:8080/websocket"); ws.onopen = function() { ws.send("Hello, world"); }; ws.onmessage = function (evt) { console.log(evt.data); }; BOTTLE – GEVENT - STRAIGHT WEBSOCKET
  4. Python class HWNamespace(BaseNamespace): def on_from_client(self, message): return ”Message from server”

    @app.route('/socket.io/<path:remaining>') def handle_websocket(remaining): socketio_manage(request.environ, {'/ hello': HWNamespace}, request) return Response() @app.route('/') def main(): return render_template('index.html') SocketIOServer(app, resource="socket.io").serve_forever() Javascript var socket = io.connect("/hello"); socket.on('connect', function () { console.log('connected'); socket.emit(’from_client', 'Hello, socket.io!', function(response) { console.log(response); }); }); FLASK – GEVENT – SOCKET.IO
  5. Coroutines (greenlets) ¡ Asynchronous functions ¡ Pause a function ¡ Comes from Stackless

    Python Callbacks ¡ Initial request tells Python to call a certain function when condition met ¡ Futures ¡ Generators ASYNCHRONOUS
  6. Client ¡ SockJS ¡ Backbone Async application ¡ Redis (Pub/Sub) ¡ Tornado SAMPLE COMPLETE

    STACK WSGI application ¡ Django ¡ PostgreSQL ¡ Redis (caching) Server ¡ ZeroMQ: messaging ¡ CloudFlare: CDN ¡ Nginx § Load balancer § Server) ¡ Ubuntu
  7. ¡  http://blog.pythonisito.com/2013/04/mongodb-pubsub-with-capped- collections.html ¡  http://mrjoes.github.io/2013/06/21/python-realtime.html ¡  http://blog.kgriffs.com/2012/12/18/uwsgi-vs-gunicorn-vs-node- benchmarks.html ¡  http://www.javaworld.com/article/2071232/java-app-dev/9-killer-uses-for-

    websockets.html ¡  https://groups.google.com/forum/#!topic/sockjs/lgzxVnlth54 ¡  https://github.com/LearnBoost/socket.io/issues/ 193#issuecomment-7308865 ¡  http://mrjoes.github.io/2011/12/15/sockjs-bench.html ¡  http://blog.pythonanywhere.com/27 RESOURCES