Skip to main content
https://travis-ci.org/invisibleroads/socketIO-client.svg?branch=0.5.7.4

socketIO-client

Here is a socket.io client library for Python. You can use it to write test code for your socket.io server.

Please note that this version implements socket.io protocol 0, which is compatible with gevent-socketio. If you want to communicate using socket.io protocol 1, please use socketIO-client 0.8.0 or higher.

Installation

Install the package in an isolated environment.

VIRTUAL_ENV=$HOME/.virtualenvs/crosscompute
virtualenv $VIRTUAL_ENV
source $VIRTUAL_ENV/bin/activate
pip install -U socketIO-client==0.5.7.4

Test

Install additional packages if you want to run the tests.

VIRTUAL_ENV=$HOME/.virtualenvs/crosscompute
export NODE_PATH=$VIRTUAL_ENV/lib/node_modules
export NPM_CONFIG_PREFIX=$VIRTUAL_ENV
npm install -g socket.io@0

Launch test server and run tests.:

cd ~/Documents
git clone https://github.com/invisibleroads/socketIO-client
git checkout -t 0.5.7.4
cd socketIO-client
node serve-tests.js  # Start socket.io server in terminal one
nosetests  # Run tests in terminal two

Usage

Activate isolated environment.

VIRTUAL_ENV=$HOME/.virtualenvs/crosscompute
source $VIRTUAL_ENV/bin/activate
export NODE_PATH=$VIRTUAL_ENV/lib/node_modules
export NPM_CONFIG_PREFIX=$VIRTUAL_ENV

Start your socket.io server.

node serve-tests.js

For debugging information, run these commands first.

import logging
logging.basicConfig(level=logging.DEBUG)

Emit.

from socketIO_client import SocketIO, LoggingNamespace

with SocketIO('localhost', 8000, LoggingNamespace) as socketIO:
    socketIO.emit('aaa')
    socketIO.wait(seconds=1)

Emit with callback.

from socketIO_client import SocketIO, LoggingNamespace

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

with SocketIO('localhost', 8000, LoggingNamespace) as socketIO:
    socketIO.emit('bbb', {'xxx': 'yyy'}, on_bbb_response)
    socketIO.wait_for_callbacks(seconds=1)

Define events.

from socketIO_client import SocketIO, LoggingNamespace

def on_connect():
    print('connect')

def on_disconnect():
    print('disconnect')

def on_reconnect():
    print('reconnect')

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

socketIO = SocketIO('localhost', 8000, LoggingNamespace)
socketIO.on('connect', on_connect)
socketIO.on('disconnect', on_disconnect)
socketIO.on('reconnect', on_reconnect)

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

Define standard events.

from socketIO_client import SocketIO, BaseNamespace

class Namespace(BaseNamespace):

    def on_connect(self):
        print('[Connected]')

    def on_reconnect(self):
        print('[Reconnected]')

    def on_disconnect(self):
        print('[Disconnected]')

socketIO = SocketIO('localhost', 8000, Namespace)
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)
chat_namespace = socketIO.define(ChatNamespace, '/chat')
news_namespace = socketIO.define(NewsNamespace, '/news')

chat_namespace.emit('aaa')
news_namespace.emit('aaa')
socketIO.wait(seconds=1)

Connect via SSL.

from socketIO_client import SocketIO

# Skip server certificate verification
SocketIO('https://localhost', verify=False)

Specify params, headers, cookies, proxies thanks to the requests library.

from socketIO_client import SocketIO

SocketIO('localhost', 8000, params={
    'q': 'qqq',
}, headers={
    'Authorization': 'Bearer xyz',
}, cookies={
    'a': 'aaa',
}, proxies={
    'https': 'https://proxy.example.com:8080',
})

Wait forever.

from socketIO_client import SocketIO

socketIO = SocketIO('localhost', 8000)
socketIO.wait()

Don’t wait forever.

from socketIO_client import SocketIO
from socketIO_client.exceptions import ConnectionError

try:
    socket = SocketIO('localhost', 8000, wait_for_connection=False)
    socket.wait()
except ConnectionError:
    print('The server is down. Try again later.')

License

This software is available under the MIT License.

Credits

0.5.7

  • Added support for Python 3.5

  • Log unicode characters properly

0.5.6

  • Backported to support requests 0.8.2

0.5.5

  • Fixed reconnection in the event of server restart

  • Fixed calling on_reconnect() so that it is actually called

  • Set default Namespace=None

  • Added support for Python 3.4

0.5.3

  • Updated wait loop to exit if the client wants to disconnect

  • Fixed calling on_connect() so that it is called only once

  • Set heartbeat_interval to be half of the heartbeat_timeout

0.5.2

  • Replaced secure=True with host=’https://example.com’

  • Fixed sending heartbeats thanks to Travis Odom

0.5.1

  • Added error handling in the event of websocket timeout

  • Fixed sending acknowledgments in custom namespaces thanks to Travis Odom

0.5

  • Rewrote library to use coroutines instead of threads to save memory

  • Improved connection resilience

  • Added support for xhr-polling thanks to Francis Bull

  • Added support for jsonp-polling thanks to Bernard Pratz

  • Added support for query params and cookies

0.4

  • Added support for custom headers and proxies thanks to Rui and Sajal

  • Added support for server-side callbacks thanks to Zac Lee

  • Added low-level _SocketIO to remove cyclic references

  • Merged Channel functionality into BaseNamespace thanks to Alexandre Bourget

0.3

  • Added support for secure connections

  • Added socketIO.wait()

  • Improved exception handling in _RhythmicThread and _ListenerThread

0.2

  • Added support for callbacks and channels thanks to Paul Kienzle

  • Incorporated suggestions from Josh VanderLinden and Ian Fitzpatrick

0.1

Metadata

Release files for socketIO-client 0.5.7.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for socketIO-client 0.5.7.4
File Size Uploaded
socketIO-client-0.5.7.4.tar.gz 13.4 kB Details

Release files / socketIO-client-0.5.7.4.tar.gz

Download URL socketIO-client-0.5.7.4.tar.gz
Size 13.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ef2e362a85ef2816fb224d727319c4b743d63b4dd9e1da99c622c9643fc4e2a0
BLAKE2b-256 checksum
How to use checksums
0f50e4ba29ea1219a986c7dd9e6b37134ab555a441107d8032fd4c4166042573
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.7.2

1 release file

0.7.1

1 release file

0.7.0

1 release file

0.6.7

1 release file

0.6.6

1 release file

0.6.5

1 release file

0.6.4

1 release file

0.6.3

1 release file

0.6.1

1 release file

This release

0.5.7.4 This release

1 release file

0.5.7.2

1 release file

0.5.7

1 release file

0.5.6

1 release file

0.5.5

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page