Socket.IO Client Library in Python

Question:

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.

Asked By: Ada

||

Answers:

Did you have a look at gevent-socketio?

Hope it helps.

Answered By: Sylvain

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_HOST = "127.0.0.1"
SOCKET_IO_PORT = 8080

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 
    gevent-socketio.
    """
    MSG_FRAME = "~m~"
    HEARTBEAT_FRAME = "~h~"
    JSON_FRAME = "~j~"

    if isinstance(message, basestring):
            encoded_msg = message
    elif isinstance(message, (object, dict)):
            return encode_for_socketio(JSON_FRAME + json.dumps(message))
    else:
            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)
ws.send(msg)
Answered By: Archie1986

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')
    conn.request('POST','/socket.io/1/')
    resp  = conn.getresponse() 
    hskey = resp.read().split(':')[0]

    self._ws = websocket.WebSocket(
                    'ws://localhost:8124/socket.io/1/websocket/'+hskey,
                    onopen   = self._onopen,
                    onmessage = self._onmessage)

....

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

Answered By: rmanna

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.

Answered By: Amit Upadhyay

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

Answered By: Sushant Khurana

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)
    socketIO.wait_for_callbacks(seconds=1)

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)
socketIO.emit('aaa')
socketIO.wait(seconds=1)

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
        self.emit('bbb')

socketIO = SocketIO('localhost', 8000)
socketIO.define(Namespace)
socketIO.emit('aaa')
socketIO.wait(seconds=1)

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')

chatNamespace.emit('aaa')
newsNamespace.emit('aaa')
socketIO.wait(seconds=1)
Answered By: Roy Hyunjin Han
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.