Cliff Dyer avatar Cliff Dyer committed 99b5cd2

Added real time web talk on gevent and socket.io

Comments (0)

Files changed (2)

 
    Kenneth Reitz: Python for Humans <python-for-humans>
    Mark Lavin: How to Maintain Your Open-Source Project (While Maintaining Your Sanity) <maintain-sanity>
-
+   real-time-web
 
 Indices and tables
 ==================

real-time-web.rst

+###################################
+Real-Time Web: Gevent and Socket.io
+###################################
+
+| Rick Copeland
+| @rick446
+| Arborian_
+
+Asyncronous IO lets you handle 10,000s of open connections
+==========================================================
+
+* Overhead is lower than threading.
+* Timeouts are easier to handle
+* GIL plus network latency make threading a poor choice.
+
+Basic asynchrony available in the standard libe via asyncore
+============================================================
+
+* Very low-level.  Lots of management.
+
+Greenlets are Stackless Python's asynchronous execution implementation in library form
+======================================================================================
+
+You don't have to install a separate interpreter, but it offers
+similar cooperative threading.
+
+Greenlets are lightweight threads that know when you might want to switch threads
+=================================================================================
+ 
+* sleeping
+* waiting on network I/O
+* looking up DNS entries
+
+Gevent wraps greenlets in useful classes
+========================================
+
+Provides tasks/semaphores/locks/etc.
+
+Gevent networking patches blocking calls in standard library
+============================================================
+
+* socket
+* ssl
+* select
+* dns
+
+Gevent StreamServer handles TCP connections by wrapping requests into greenlets
+===============================================================================
+
+.. code:: python
+    
+    def handle(socket, address):
+        print "foo"
+
+    server = StreamServer(('127.0.0.0', 8080), handle)
+    server.start()
+
+SocketIO abstracts methods of providing real-time responsiveness
+================================================================
+
+basic socket.io
+
+.. code:: html
+
+    <script src='/socket.io/socket.io.js"></script>
+    <script>
+        var socket = io.connect('/chat');
+        socket.on('some_event', function(data) {
+        console.log(data);
+        socket.emit('chat', {'my': data});
+    }
+    </script>
+
+gevent_socketio provides WSGI-side responses to socketio messages
+=================================================================
+
+.. code:: python
+    
+    def hello_world(environ, start_response):
+        return socketio_manage(environ, { '/chat': ChatNamespace})
+
+    class ChatNamespace(BaseNamespace):
+        def initialize(self):
+            # when connection is established
+            pass
+        def on_chat(self, message):
+            self.emit('some_event', message_body)
+
+
+SocketIO can be used to build a web-based chat client
+=====================================================
+
+Incomplete snippet (too much code to keep up)
+Full code is `available online`__
+
+.. __: http://github.com/rick446/Chatterbox
+
+.. code:: html
+
+    socket.on('chat/', function(msg) {
+        $output.html($output.html() + '<br>' + msg);
+    });
+    socket.on('connect/', function(sender) {
+        $output.html("Connected to " + sender);
+    });
+
+
+
+.. _Arborian: http://arborian.com/
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.