Slide 1

Slide 1 text

Michael Irani REAL TIME WEB SANS NODE 3.18.2014

Slide 2

Slide 2 text

•  Blogs •  Hacker News •  News sites •  Flickr •  Craigslist •  Wikipedia •  Pandora •  Ebay HTTP – BUILT FOR REQUESTS

Slide 3

Slide 3 text

¡ Social feeds ¡ Site usage ¡ Sports scores ¡ Stock ticker ¡ Collaborative editing ¡ Chat ¡ Multiplayer games ¡ Server/process data BIDERECTIONAL COMMUNICATION

Slide 4

Slide 4 text

¡ Long polling ¡ TCP, keep-alive header ¡ Chunked transfer encoding ¡ Forever iframe ¡ XMLHttpRequest (XHR) Ajax Push COMET

Slide 5

Slide 5 text

Bidirectional Server-client TCP connection WEBSOCKETS

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

¡ SockJS ¡ Socket.IO ¡ Faye ¡ Engine.IO ¡ Autobahn Code that provides technology you’d expect a browser to have natively POLYFILL LIBRARIES

Slide 8

Slide 8 text

Python class HWNamespace(BaseNamespace): def on_from_client(self, message): return ”Message from server” @app.route('/socket.io/') 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

Slide 9

Slide 9 text

EXAMPLE – TORNADO W/ SOCKJS

Slide 10

Slide 10 text

WSGI issues PYTHON

Slide 11

Slide 11 text

¡ Synchronous ¡ Only one request at a time WSGI – WEB SERVER GATEWAY INTERFACE

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

¡ GEvent ¡ Tornado ¡ Twisted ¡ Cyclone ¡ Eventlet ¡ geventwebsocket ¡ gevent-socketio ¡ tornadio ¡ Tornadio2 ¡ sockjs-tornado ¡ SockJS-Twisted ¡ SockJS-cyclone PYTHON OPTIONS

Slide 14

Slide 14 text

GEvent ¡ gevent-socketio ¡ gevent-websocket Tornado ¡ sockjs-tornado ACTUAL PYTHON OPTIONS

Slide 15

Slide 15 text

¡ RabbitMQ ¡ ActiveMQ ¡ Redis ¡ ZeroMQ MESSAGING

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

¡ Autobahn: Twisted, asyncio, WAMP ¡ WAMP: Web Application Messaging Protocol ¡ Brubeck: Mongrel2, 0MQ, Redis, GEvent THINGS TO LOOK INTO

Slide 18

Slide 18 text

¡ NYTimes ¡ StackOverflow ¡ http://kaazing.com ¡ http://playbuildy.com ¡ http://pythonanywhere.com ¡ http://hubdoc.com SITES USING WEBSOCKETS

Slide 19

Slide 19 text

¡ Wireshark: “websocket” filter ¡ Chrome: developer tools, Network, 101 response codes TOOLS

Slide 20

Slide 20 text

¡  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

Slide 21

Slide 21 text

@wowzer GITHUB.COM/WOWZER/RTW SPEAKERDECK.COM/WOWZER/ REAL-TIME-WEB-SANS-NODE