Take the 2-minute tour ×

Can anyone recommend a Socket.IO client library for Python? I've had a look around, but the only ones I can find are either server implementations, or depend on a framework such as Twisted.

I need a client library that has no dependencies on other frameworks.

Simply using one of the many connection types isn't sufficient, as the python client will need to work with multiple socketio servers, many of which won't support websockets, for example.

share|improve this question
add comment

6 Answers

up vote 28 down vote accepted

Archie1986's answer was good but has become outdated with socketio updates (more specifically, its protocol : https://github.com/LearnBoost/socket.io-spec)... as far as i can tell, you need to perform the handshake manually before you can ask for a transport (e.g., websockets) connection... note that the code below is incomplete and insecure... for one, it ignores the list of supported transports returned in the handshake response and always tries to get a websocket... also it assumes that the handshake always succeeds... nevertheless, it's a good place to start

import websocket, httplib


    connect to the socketio server

    1. perform the HTTP handshake
    2. open a websocket connection '''
def connect(self) :
    conn  = httplib.HTTPConnection('localhost:8124')
    resp  = conn.getresponse() 
    hskey = resp.read().split(':')[0]

    self._ws = websocket.WebSocket(
                    onopen   = self._onopen,
                    onmessage = self._onmessage)


you might also want to read up on python-websockets: https://github.com/mtah/python-websocket

share|improve this answer
This works great for me. Thanks so much for the updated answer! I wish the OP would re-promote this one, because the actual answer (as you point out) is not going to actually work with the current spec. I filled out the rest of the code for a simple command line tool that works nicely. It might be useful to someone else: gist.github.com/1402238 It's basically just your code with some very minor other stuff that's mostly obvious, but I hope it'll save someone some time. –  drewww Nov 28 '11 at 21:48
add comment

First of all, I'm not sure why some of your Socket.IO servers won't support websockets...the intent of Socket.IO is to make front-end browser development of web apps easier by providing an abstracted interface to real-time data streams being served up by the Socket.IO server. Perhaps Socket.IO is not what you should be using for your application? That aside, let me try to answer your question...

At this point in time, there aren't any Socket.IO client libraries for Python (gevent-socketio is not a Socket.IO client library for Python...it is a Socket.IO server library for Python). For now, you are going to have to piece some original code together in order to interface with Socket.IO directly as a client while accepting various connection types.

I know you are looking for a cure-all that works across various connection types (WebSocket, long-polling, etc.), but since a library such as this does not exist as of yet, I can at least give you some guidance on using the WebSocket connection type based on my experience.

For the WebSocket connection type, create a WebSocket client in Python. From the command line install this Python WebSocket Client package here with pip so that it is on your python path like so:

pip install -e git+https://github.com/liris/websocket-client.git#egg=websocket

Once you've done that try the following, replacing SOCKET_IO_HOST and SOCKET_IO_PORT with the appropriate location of your Socket.IO server:

import websocket


socket_io_url = 'ws://' + SOCKET_IO_HOST + ':' + str(SOCKET_IO_PORT) + '/socket.io/websocket'

ws = websocket.create_connection(socket_io_url)

At this point you have a medium of interfacing with a Socket.IO server directly from Python. To send messages to the Socket.IO server simply send a message through this WebSocket connection. In order for the Socket.IO server to properly interpret incoming messages through this WebSocket from your Python Socket.IO client, you need to adhere to the Socket.IO protocol and encode any strings or dictionaries you might send through the WebSocket connection. For example, after you've accomplished everything above do the following:

def encode_for_socketio(message):
    Encode 'message' string or dictionary to be able
    to be transported via a Python WebSocket client to 
    a Socket.IO server (which is capable of receiving 
    WebSocket communications). This method taken from 
    MSG_FRAME = "~m~"
    JSON_FRAME = "~j~"

    if isinstance(message, basestring):
            encoded_msg = message
    elif isinstance(message, (object, dict)):
            return encode_for_socketio(JSON_FRAME + json.dumps(message))
            raise ValueError("Can't encode message.")

    return MSG_FRAME + str(len(encoded_msg)) + MSG_FRAME + encoded_msg

msg = "Hello, world!"
msg = encode_for_socketio(msg)
share|improve this answer
full example: pastebin.com/1Xt6X4K5 - but I'm not really using HTTP exactly, I had to modify websocket.py like this: pastebin.com/rh9WxxJA because, I suspect my version of urlparse doesn't recognise 'ws://' web socket protocol. –  Luke Stanley Jul 8 '11 at 12:35
add comment

The socketIO-client library supports event callbacks and channels thanks to the work of contributors and is available on PyPI under the MIT license.

Emit with callback.

from socketIO_client import SocketIO

def on_bbb_response(*args):
    print 'on_bbb_response', args

with SocketIO('localhost', 8000) as socketIO:
    socketIO.emit('bbb', {'xxx': 'yyy'}, on_bbb_response)

Define events.

from socketIO_client import SocketIO

def on_aaa_response(*args):
    print 'on_aaa_response', args

socketIO = SocketIO('localhost', 8000)
socketIO.on('aaa_response', on_aaa_response)

Define events in a namespace.

from socketIO_client import SocketIO, BaseNamespace

class Namespace(BaseNamespace):

    def on_aaa_response(self, *args):
        print 'on_aaa_response', args

socketIO = SocketIO('localhost', 8000)

Define different namespaces on a single socket.

from socketIO_client import SocketIO, BaseNamespace

class ChatNamespace(BaseNamespace):

    def on_aaa_response(self, *args):
        print 'on_aaa_response', args

class NewsNamespace(BaseNamespace):

    def on_aaa_response(self, *args):
        print 'on_aaa_response', args

socketIO = SocketIO('localhost', 8000)
chatNamespace = socketIO.define(ChatNamespace, '/chat')
newsNamespace = socketIO.define(NewsNamespace, '/news')

share|improve this answer
Heh, we've come full circle, seeing as the socketIO-client package was spawned from the related SO question: stackoverflow.com/questions/6692908/… –  Maxy-B Sep 13 '12 at 3:44
add comment

Did you have a look at gevent-socketio?

Hope it helps.

share|improve this answer
gevent-socketio is not a Socket.IO client library for Python...it is a Socket.IO server library for Python –  Archie1986 Jun 12 '11 at 19:39
+1 It really helped me, thanks. –  Tom Leys Apr 26 '12 at 3:34
add comment

Wrote one: https://github.com/amitu/amitu-websocket-client/blob/master/amitu/socketio_client.py. It only supports websockets so it may have only marginal utility for you.

share|improve this answer
when executing the run() method i get an error: Invalid frame - what could that mean? –  Alp Jun 6 '12 at 15:31
Alp, it means there is a bug in the library. I have updated it based on pulls from various contributors. Please feel free to check the latest version and see if it helps. –  Amit Upadhyay Sep 6 '12 at 21:54
Thanks for your reply. In the meantime i switched to sockjs, which works like a charm. See stackoverflow.com/questions/10950365/… –  Alp Sep 6 '12 at 22:36
add comment

The SocketTornad.IO library with the popular asynchronous Tornado Web Server is also one of the options available for python.

share|improve this answer
or even better, the more current version of Tornadio2: github.com/mrjoes/tornadIO2 --- but these are server libs, not intended for use as a client. –  Alp Jun 8 '12 at 11:34
add comment

Your Answer


