A socket.io bridge for Python
Switch branches/tags
Nothing to show
Latest commit fb62996 Aug 10, 2011 evan renamed io.py to socket_io.py to avoid standard library conflict, add…
…ed package.json to specify correct version of socket.io (0.7 changed the API)


A socket.io bridge for Python

This gives Python users access to socket.io, a node.js library. This library provides simple and efficient bidirectional communication between browsers and servers over a message-oriented socket. Transport is normalized over various technologies including WebSockets, Flash sockets, and AJAX polling.


This bridge requires node.js and socket.io. Install node.js and npm, then run npm install . in this directory to install the correct version of socket.io (the newer versions have changed their API).


This bridge is designed to be self-contained, so socket_io.py is the only file you need. A server is created by subclassing socket_io.Socket and overriding the on_connect, on_message, and/or on_disconnect methods:

import socket_io as io

class Server(io.Server):
    def on_connect(self, client):
        print client, 'connected'
        self.broadcast(str(client) + ' connected')
        print 'there are now', len(self.clients), 'clients'
    def on_message(self, client, message):
        print client, 'sent', message
    def on_disconnect(self, client):
        print client, 'disconnected'
        self.broadcast(str(client) + ' disconnected')
        print 'there are now', len(self.clients), 'clients'


The client in the browser just uses the same interface that regular socket.io clients use:

<script type="text/javascript" src="http://localhost:5000/socket.io/socket.io.js"></script>
<script type="text/javascript">

function log(html) {
    document.body.innerHTML += html + '<br>';

var socket = new io.Socket('localhost', { port: 5000 });
socket.on('connect', function() {
socket.on('message', function(data) {
    log('message: ' + data);
socket.on('disconnect', function() {