API Reference — python-socketio documentation (original) (raw)

class socketio.SimpleClient(*args, **kwargs)

A Socket.IO client.

This class implements a simple, yet fully compliant Socket.IO web client with support for websocket and long-polling transports.

The positional and keyword arguments given in the constructor are passed to the underlying socketio.Client() object.

call(event, data=None, timeout=60)

Emit an event to the server and wait for a response.

This method issues an emit and waits for the server to provide a response or acknowledgement. If the response does not arrive before the timeout, then a TimeoutError exception is raised.

Parameters:

client_class

alias of Client

connect(url, headers={}, auth=None, transports=None, namespace='/', socketio_path='socket.io', wait_timeout=5)

Connect to a Socket.IO server.

Parameters:

disconnect()

Disconnect from the server.

emit(event, data=None)

Emit an event to the server.

Parameters:

This method schedules the event to be sent out and returns, without actually waiting for its delivery. In cases where the client needs to ensure that the event was received, socketio.SimpleClient.call()should be used instead.

receive(timeout=None)

Wait for an event from the server.

Parameters:

timeout – The waiting timeout. If the timeout is reached before the server acknowledges the event, then aTimeoutError exception is raised.

The return value is a list with the event name as the first element. If the server included arguments with the event, they are returned as additional list elements.

property sid

The session ID received from the server.

The session ID is not guaranteed to remain constant throughout the life of the connection, as reconnections can cause it to change.

property transport

The name of the transport currently in use.

The transport is returned as a string and can be one of pollingand websocket.

class socketio.AsyncSimpleClient(*args, **kwargs)

A Socket.IO client.

This class implements a simple, yet fully compliant Socket.IO web client with support for websocket and long-polling transports.

The positional and keyword arguments given in the constructor are passed to the underlying socketio.AsyncClient() object.

async call(event, data=None, timeout=60)

Emit an event to the server and wait for a response.

This method issues an emit and waits for the server to provide a response or acknowledgement. If the response does not arrive before the timeout, then a TimeoutError exception is raised.

Parameters:

Note: this method is a coroutine.

client_class

alias of AsyncClient

async connect(url, headers={}, auth=None, transports=None, namespace='/', socketio_path='socket.io', wait_timeout=5)

Connect to a Socket.IO server.

Parameters:

Note: this method is a coroutine.

async disconnect()

Disconnect from the server.

Note: this method is a coroutine.

async emit(event, data=None)

Emit an event to the server.

Parameters:

Note: this method is a coroutine.

This method schedules the event to be sent out and returns, without actually waiting for its delivery. In cases where the client needs to ensure that the event was received, socketio.SimpleClient.call()should be used instead.

async receive(timeout=None)

Wait for an event from the server.

Parameters:

timeout – The waiting timeout. If the timeout is reached before the server acknowledges the event, then aTimeoutError exception is raised.

Note: this method is a coroutine.

The return value is a list with the event name as the first element. If the server included arguments with the event, they are returned as additional list elements.

property sid

The session ID received from the server.

The session ID is not guaranteed to remain constant throughout the life of the connection, as reconnections can cause it to change.

property transport

The name of the transport currently in use.

The transport is returned as a string and can be one of pollingand websocket.

class socketio.Client(reconnection=True, reconnection_attempts=0, reconnection_delay=1, reconnection_delay_max=5, randomization_factor=0.5, logger=False, serializer='default', json=None, handle_sigint=True, **kwargs)

A Socket.IO client.

This class implements a fully compliant Socket.IO web client with support for websocket and long-polling transports.

Parameters:

The Engine.IO configuration supports the following settings:

Parameters:

call(event, data=None, namespace=None, timeout=60)

Emit a custom event to the server and wait for the response.

This method issues an emit with a callback and waits for the callback to be invoked before returning. If the callback isn’t invoked before the timeout, then a TimeoutError exception is raised. If the Socket.IO connection drops during the wait, this method still waits until the specified timeout.

Parameters:

Note: this method is not thread safe. If multiple threads are emitting at the same time on the same client connection, messages composed of multiple packets may end up being sent in an incorrect sequence. Use standard concurrency solutions (such as a Lock object) to prevent this situation.

connect(url, headers={}, auth=None, transports=None, namespaces=None, socketio_path='socket.io', wait=True, wait_timeout=1, retry=False)

Connect to a Socket.IO server.

Parameters:

Example usage:

sio = socketio.Client() sio.connect('http://localhost:5000')

connected

Indicates if the client is connected or not.

disconnect()

Disconnect from the server.

emit(event, data=None, namespace=None, callback=None)

Emit a custom event to the server.

Parameters:

Note: this method is not thread safe. If multiple threads are emitting at the same time on the same client connection, messages composed of multiple packets may end up being sent in an incorrect sequence. Use standard concurrency solutions (such as a Lock object) to prevent this situation.

event(*args, **kwargs)

Decorator to register an event handler.

This is a simplified version of the on() method that takes the event name from the decorated function.

Example usage:

@sio.event def my_event(data): print('Received data: ', data)

The above example is equivalent to:

@sio.on('my_event') def my_event(data): print('Received data: ', data)

A custom namespace can be given as an argument to the decorator:

@sio.event(namespace='/test') def my_event(data): print('Received data: ', data)

get_sid(namespace=None)

Return the sid associated with a connection.

Parameters:

namespace – The Socket.IO namespace. If this argument is omitted the handler is associated with the default namespace. Note that unlike previous versions, the current version of the Socket.IO protocol uses different sid values per namespace.

This method returns the sid for the requested namespace as a string.

namespaces

set of connected namespaces.

on(event, handler=None, namespace=None)

Register an event handler.

Parameters:

Example usage:

as a decorator:

@sio.on('connect') def connect_handler(): print('Connected!')

as a method:

def message_handler(msg): print('Received message: ', msg) sio.send( 'response') sio.on('message', message_handler)

The arguments passed to the handler function depend on the event type:

class reason

Disconnection reasons.

CLIENT_DISCONNECT = 'client disconnect'

Client-initiated disconnection.

SERVER_DISCONNECT = 'server disconnect'

Server-initiated disconnection.

TRANSPORT_ERROR = 'transport error'

Transport error.

register_namespace(namespace_handler)

Register a namespace handler object.

Parameters:

namespace_handler – An instance of a Namespacesubclass that handles all the event traffic for a namespace.

send(data, namespace=None, callback=None)

Send a message to the server.

This function emits an event with the name 'message'. Useemit() to issue custom event names.

Parameters:

shutdown()

Stop the client.

If the client is connected to a server, it is disconnected. If the client is attempting to reconnect to server, the reconnection attempts are stopped. If the client is not connected to a server and is not attempting to reconnect, then this function does nothing.

sleep(seconds=0)

Sleep for the requested amount of time using the appropriate async model.

This is a utility function that applications can use to put a task to sleep without having to worry about using the correct call for the selected async mode.

start_background_task(target, *args, **kwargs)

Start a background task using the appropriate async model.

This is a utility function that applications can use to start a background task using the method that is compatible with the selected async mode.

Parameters:

This function returns an object that represents the background task, on which the join() methond can be invoked to wait for the task to complete.

transport()

Return the name of the transport used by the client.

The two possible values returned by this function are 'polling'and 'websocket'.

wait()

Wait until the connection with the server ends.

Client applications can use this function to block the main thread during the life of the connection.

class socketio.AsyncClient(reconnection=True, reconnection_attempts=0, reconnection_delay=1, reconnection_delay_max=5, randomization_factor=0.5, logger=False, serializer='default', json=None, handle_sigint=True, **kwargs)

A Socket.IO client for asyncio.

This class implements a fully compliant Socket.IO web client with support for websocket and long-polling transports.

Parameters:

The Engine.IO configuration supports the following settings:

Parameters:

async call(event, data=None, namespace=None, timeout=60)

Emit a custom event to the server and wait for the response.

This method issues an emit with a callback and waits for the callback to be invoked before returning. If the callback isn’t invoked before the timeout, then a TimeoutError exception is raised. If the Socket.IO connection drops during the wait, this method still waits until the specified timeout.

Parameters:

Note: this method is not designed to be used concurrently. If multiple tasks are emitting at the same time on the same client connection, then messages composed of multiple packets may end up being sent in an incorrect sequence. Use standard concurrency solutions (such as a Lock object) to prevent this situation.

Note 2: this method is a coroutine.

async connect(url, headers={}, auth=None, transports=None, namespaces=None, socketio_path='socket.io', wait=True, wait_timeout=1, retry=False)

Connect to a Socket.IO server.

Parameters:

Note: this method is a coroutine.

Example usage:

sio = socketio.AsyncClient() await sio.connect('http://localhost:5000')

connected

Indicates if the client is connected or not.

async disconnect()

Disconnect from the server.

Note: this method is a coroutine.

async emit(event, data=None, namespace=None, callback=None)

Emit a custom event to the server.

Parameters:

Note: this method is not designed to be used concurrently. If multiple tasks are emitting at the same time on the same client connection, then messages composed of multiple packets may end up being sent in an incorrect sequence. Use standard concurrency solutions (such as a Lock object) to prevent this situation.

Note 2: this method is a coroutine.

event(*args, **kwargs)

Decorator to register an event handler.

This is a simplified version of the on() method that takes the event name from the decorated function.

Example usage:

@sio.event def my_event(data): print('Received data: ', data)

The above example is equivalent to:

@sio.on('my_event') def my_event(data): print('Received data: ', data)

A custom namespace can be given as an argument to the decorator:

@sio.event(namespace='/test') def my_event(data): print('Received data: ', data)

get_sid(namespace=None)

Return the sid associated with a connection.

Parameters:

namespace – The Socket.IO namespace. If this argument is omitted the handler is associated with the default namespace. Note that unlike previous versions, the current version of the Socket.IO protocol uses different sid values per namespace.

This method returns the sid for the requested namespace as a string.

namespaces

set of connected namespaces.

on(event, handler=None, namespace=None)

Register an event handler.

Parameters:

Example usage:

as a decorator:

@sio.on('connect') def connect_handler(): print('Connected!')

as a method:

def message_handler(msg): print('Received message: ', msg) sio.send( 'response') sio.on('message', message_handler)

The arguments passed to the handler function depend on the event type:

class reason

Disconnection reasons.

CLIENT_DISCONNECT = 'client disconnect'

Client-initiated disconnection.

SERVER_DISCONNECT = 'server disconnect'

Server-initiated disconnection.

TRANSPORT_ERROR = 'transport error'

Transport error.

register_namespace(namespace_handler)

Register a namespace handler object.

Parameters:

namespace_handler – An instance of a Namespacesubclass that handles all the event traffic for a namespace.

async send(data, namespace=None, callback=None)

Send a message to the server.

This function emits an event with the name 'message'. Useemit() to issue custom event names.

Parameters:

Note: this method is a coroutine.

async shutdown()

Stop the client.

If the client is connected to a server, it is disconnected. If the client is attempting to reconnect to server, the reconnection attempts are stopped. If the client is not connected to a server and is not attempting to reconnect, then this function does nothing.

async sleep(seconds=0)

Sleep for the requested amount of time using the appropriate async model.

This is a utility function that applications can use to put a task to sleep without having to worry about using the correct call for the selected async mode.

Note: this method is a coroutine.

start_background_task(target, *args, **kwargs)

Start a background task using the appropriate async model.

This is a utility function that applications can use to start a background task using the method that is compatible with the selected async mode.

Parameters:

The return value is a asyncio.Task object.

transport()

Return the name of the transport used by the client.

The two possible values returned by this function are 'polling'and 'websocket'.

async wait()

Wait until the connection with the server ends.

Client applications can use this function to block the main thread during the life of the connection.

Note: this method is a coroutine.

class socketio.Server(client_manager=None, logger=False, serializer='default', json=None, async_handlers=True, always_connect=False, namespaces=None, **kwargs)

A Socket.IO server.

This class implements a fully compliant Socket.IO web server with support for websocket and long-polling transports.

Parameters:

The Engine.IO configuration supports the following settings:

Parameters:

call(event, data=None, to=None, sid=None, namespace=None, timeout=60, ignore_queue=False)

Emit a custom event to a client and wait for the response.

This method issues an emit with a callback and waits for the callback to be invoked before returning. If the callback isn’t invoked before the timeout, then a TimeoutError exception is raised. If the Socket.IO connection drops during the wait, this method still waits until the specified timeout.

Parameters:

Note: this method is not thread safe. If multiple threads are emitting at the same time to the same client, then messages composed of multiple packets may end up being sent in an incorrect sequence. Use standard concurrency solutions (such as a Lock object) to prevent this situation.

close_room(room, namespace=None)

Close a room.

This function removes all the clients from the given room.

Parameters:

disconnect(sid, namespace=None, ignore_queue=False)

Disconnect a client.

Parameters:

emit(event, data=None, to=None, room=None, skip_sid=None, namespace=None, callback=None, ignore_queue=False)

Emit a custom event to one or more connected clients.

Parameters:

Note: this method is not thread safe. If multiple threads are emitting at the same time to the same client, then messages composed of multiple packets may end up being sent in an incorrect sequence. Use standard concurrency solutions (such as a Lock object) to prevent this situation.

enter_room(sid, room, namespace=None)

Enter a room.

This function adds the client to a room. The emit() andsend() functions can optionally broadcast events to all the clients in a room.

Parameters:

event(*args, **kwargs)

Decorator to register an event handler.

This is a simplified version of the on() method that takes the event name from the decorated function.

Example usage:

@sio.event def my_event(data): print('Received data: ', data)

The above example is equivalent to:

@sio.on('my_event') def my_event(data): print('Received data: ', data)

A custom namespace can be given as an argument to the decorator:

@sio.event(namespace='/test') def my_event(data): print('Received data: ', data)

get_environ(sid, namespace=None)

Return the WSGI environ dictionary for a client.

Parameters:

get_session(sid, namespace=None)

Return the user session for a client.

Parameters:

The return value is a dictionary. Modifications made to this dictionary are not guaranteed to be preserved unlesssave_session() is called, or when the session context manager is used.

handle_request(environ, start_response)

Handle an HTTP request from the client.

This is the entry point of the Socket.IO application, using the same interface as a WSGI application. For the typical usage, this function is invoked by the Middleware instance, but it can be invoked directly when the middleware is not used.

Parameters:

This function returns the HTTP response body to deliver to the client as a byte sequence.

instrument(auth=None, mode='development', read_only=False, server_id=None, namespace='/admin', server_stats_interval=2)

Instrument the Socket.IO server for monitoring with the Socket.IO Admin UI.

Parameters:

leave_room(sid, room, namespace=None)

Leave a room.

This function removes the client from a room.

Parameters:

on(event, handler=None, namespace=None)

Register an event handler.

Parameters:

Example usage:

as a decorator:

@sio.on('connect', namespace='/chat') def connect_handler(sid, environ): print('Connection request') if environ['REMOTE_ADDR'] in blacklisted: return False # reject

as a method:

def message_handler(sid, msg): print('Received message: ', msg) sio.send(sid, 'response') socket_io.on('message', namespace='/chat', handler=message_handler)

The arguments passed to the handler function depend on the event type:

class reason

Disconnection reasons.

CLIENT_DISCONNECT = 'client disconnect'

Client-initiated disconnection.

PING_TIMEOUT = 'ping timeout'

Ping timeout.

SERVER_DISCONNECT = 'server disconnect'

Server-initiated disconnection.

TRANSPORT_CLOSE = 'transport close'

Transport close.

TRANSPORT_ERROR = 'transport error'

Transport error.

register_namespace(namespace_handler)

Register a namespace handler object.

Parameters:

namespace_handler – An instance of a Namespacesubclass that handles all the event traffic for a namespace.

rooms(sid, namespace=None)

Return the rooms a client is in.

Parameters:

save_session(sid, session, namespace=None)

Store the user session for a client.

Parameters:

send(data, to=None, room=None, skip_sid=None, namespace=None, callback=None, ignore_queue=False)

Send a message to one or more connected clients.

This function emits an event with the name 'message'. Useemit() to issue custom event names.

Parameters:

session(sid, namespace=None)

Return the user session for a client with context manager syntax.

Parameters:

sid – The session id of the client.

This is a context manager that returns the user session dictionary for the client. Any changes that are made to this dictionary inside the context manager block are saved back to the session. Example usage:

@sio.on('connect') def on_connect(sid, environ): username = authenticate_user(environ) if not username: return False with sio.session(sid) as session: session['username'] = username

@sio.on('message') def on_message(sid, msg): with sio.session(sid) as session: print('received message from ', session['username'])

shutdown()

Stop Socket.IO background tasks.

This method stops all background activity initiated by the Socket.IO server. It must be called before shutting down the web server.

sleep(seconds=0)

Sleep for the requested amount of time using the appropriate async model.

This is a utility function that applications can use to put a task to sleep without having to worry about using the correct call for the selected async mode.

start_background_task(target, *args, **kwargs)

Start a background task using the appropriate async model.

This is a utility function that applications can use to start a background task using the method that is compatible with the selected async mode.

Parameters:

This function returns an object that represents the background task, on which the join() methond can be invoked to wait for the task to complete.

transport(sid, namespace=None)

Return the name of the transport used by the client.

The two possible values returned by this function are 'polling'and 'websocket'.

Parameters:

class socketio.AsyncServer(client_manager=None, logger=False, json=None, async_handlers=True, namespaces=None, **kwargs)

A Socket.IO server for asyncio.

This class implements a fully compliant Socket.IO web server with support for websocket and long-polling transports, compatible with the asyncio framework.

Parameters:

The Engine.IO configuration supports the following settings:

Parameters:

attach(app, socketio_path='socket.io')

Attach the Socket.IO server to an application.

async call(event, data=None, to=None, sid=None, namespace=None, timeout=60, ignore_queue=False)

Emit a custom event to a client and wait for the response.

This method issues an emit with a callback and waits for the callback to be invoked before returning. If the callback isn’t invoked before the timeout, then a TimeoutError exception is raised. If the Socket.IO connection drops during the wait, this method still waits until the specified timeout.

Parameters:

Note: this method is not designed to be used concurrently. If multiple tasks are emitting at the same time to the same client connection, then messages composed of multiple packets may end up being sent in an incorrect sequence. Use standard concurrency solutions (such as a Lock object) to prevent this situation.

Note 2: this method is a coroutine.

async close_room(room, namespace=None)

Close a room.

This function removes all the clients from the given room.

Parameters:

Note: this method is a coroutine.

async disconnect(sid, namespace=None, ignore_queue=False)

Disconnect a client.

Parameters:

Note: this method is a coroutine.

async emit(event, data=None, to=None, room=None, skip_sid=None, namespace=None, callback=None, ignore_queue=False)

Emit a custom event to one or more connected clients.

Parameters:

Note: this method is not designed to be used concurrently. If multiple tasks are emitting at the same time to the same client connection, then messages composed of multiple packets may end up being sent in an incorrect sequence. Use standard concurrency solutions (such as a Lock object) to prevent this situation.

Note 2: this method is a coroutine.

async enter_room(sid, room, namespace=None)

Enter a room.

This function adds the client to a room. The emit() andsend() functions can optionally broadcast events to all the clients in a room.

Parameters:

Note: this method is a coroutine.

event(*args, **kwargs)

Decorator to register an event handler.

This is a simplified version of the on() method that takes the event name from the decorated function.

Example usage:

@sio.event def my_event(data): print('Received data: ', data)

The above example is equivalent to:

@sio.on('my_event') def my_event(data): print('Received data: ', data)

A custom namespace can be given as an argument to the decorator:

@sio.event(namespace='/test') def my_event(data): print('Received data: ', data)

get_environ(sid, namespace=None)

Return the WSGI environ dictionary for a client.

Parameters:

async get_session(sid, namespace=None)

Return the user session for a client.

Parameters:

The return value is a dictionary. Modifications made to this dictionary are not guaranteed to be preserved. If you want to modify the user session, use the session context manager instead.

async handle_request(*args, **kwargs)

Handle an HTTP request from the client.

This is the entry point of the Socket.IO application. This function returns the HTTP response body to deliver to the client.

Note: this method is a coroutine.

instrument(auth=None, mode='development', read_only=False, server_id=None, namespace='/admin', server_stats_interval=2)

Instrument the Socket.IO server for monitoring with the Socket.IO Admin UI.

Parameters:

async leave_room(sid, room, namespace=None)

Leave a room.

This function removes the client from a room.

Parameters:

Note: this method is a coroutine.

on(event, handler=None, namespace=None)

Register an event handler.

Parameters:

Example usage:

as a decorator:

@sio.on('connect', namespace='/chat') def connect_handler(sid, environ): print('Connection request') if environ['REMOTE_ADDR'] in blacklisted: return False # reject

as a method:

def message_handler(sid, msg): print('Received message: ', msg) sio.send(sid, 'response') socket_io.on('message', namespace='/chat', handler=message_handler)

The arguments passed to the handler function depend on the event type:

class reason

Disconnection reasons.

CLIENT_DISCONNECT = 'client disconnect'

Client-initiated disconnection.

PING_TIMEOUT = 'ping timeout'

Ping timeout.

SERVER_DISCONNECT = 'server disconnect'

Server-initiated disconnection.

TRANSPORT_CLOSE = 'transport close'

Transport close.

TRANSPORT_ERROR = 'transport error'

Transport error.

register_namespace(namespace_handler)

Register a namespace handler object.

Parameters:

namespace_handler – An instance of a Namespacesubclass that handles all the event traffic for a namespace.

rooms(sid, namespace=None)

Return the rooms a client is in.

Parameters:

async save_session(sid, session, namespace=None)

Store the user session for a client.

Parameters:

async send(data, to=None, room=None, skip_sid=None, namespace=None, callback=None, ignore_queue=False)

Send a message to one or more connected clients.

This function emits an event with the name 'message'. Useemit() to issue custom event names.

Parameters:

Note: this method is a coroutine.

session(sid, namespace=None)

Return the user session for a client with context manager syntax.

Parameters:

sid – The session id of the client.

This is a context manager that returns the user session dictionary for the client. Any changes that are made to this dictionary inside the context manager block are saved back to the session. Example usage:

@eio.on('connect') def on_connect(sid, environ): username = authenticate_user(environ) if not username: return False with eio.session(sid) as session: session['username'] = username

@eio.on('message') def on_message(sid, msg): async with eio.session(sid) as session: print('received message from ', session['username'])

async shutdown()

Stop Socket.IO background tasks.

This method stops all background activity initiated by the Socket.IO server. It must be called before shutting down the web server.

async sleep(seconds=0)

Sleep for the requested amount of time using the appropriate async model.

This is a utility function that applications can use to put a task to sleep without having to worry about using the correct call for the selected async mode.

Note: this method is a coroutine.

start_background_task(target, *args, **kwargs)

Start a background task using the appropriate async model.

This is a utility function that applications can use to start a background task using the method that is compatible with the selected async mode.

Parameters:

The return value is a asyncio.Task object.

transport(sid, namespace=None)

Return the name of the transport used by the client.

The two possible values returned by this function are 'polling'and 'websocket'.

Parameters:

class socketio.exceptions.ConnectionRefusedError(*args)

Connection refused exception.

This exception can be raised from a connect handler when the connection is not accepted. The positional arguments provided with the exception are returned with the error packet to the client.

class socketio.WSGIApp(socketio_app, wsgi_app=None, static_files=None, socketio_path='socket.io')

WSGI middleware for Socket.IO.

This middleware dispatches traffic to a Socket.IO application. It can also serve a list of static files to the client, or forward unrelated HTTP traffic to another WSGI application.

Parameters:

Example usage:

import socketio import eventlet from . import wsgi_app

sio = socketio.Server() app = socketio.WSGIApp(sio, wsgi_app) eventlet.wsgi.server(eventlet.listen(('', 8000)), app)

class socketio.ASGIApp(socketio_server, other_asgi_app=None, static_files=None, socketio_path='socket.io', on_startup=None, on_shutdown=None)

ASGI application middleware for Socket.IO.

This middleware dispatches traffic to an Socket.IO application. It can also serve a list of static files to the client, or forward unrelated HTTP traffic to another ASGI application.

Parameters:

Example usage:

import socketio import uvicorn

sio = socketio.AsyncServer() app = socketio.ASGIApp(sio, static_files={ '/': 'index.html', '/static': './public', }) uvicorn.run(app, host='127.0.0.1', port=5000)

class socketio.Middleware(socketio_app, wsgi_app=None, socketio_path='socket.io')

This class has been renamed to WSGIApp and is now deprecated.

class socketio.ClientNamespace(namespace=None)

Base class for client-side class-based namespaces.

A class-based namespace is a class that contains all the event handlers for a Socket.IO namespace. The event handlers are methods of the class with the prefix on_, such as on_connect, on_disconnect,on_message, on_json, and so on.

Parameters:

namespace – The Socket.IO namespace to be used with all the event handlers defined in this class. If this argument is omitted, the default namespace is used.

call(event, data=None, namespace=None, timeout=None)

Emit a custom event to the server and wait for the response.

The only difference with the socketio.Client.call() method is that when the namespace argument is not given the namespace associated with the class is used.

disconnect()

Disconnect from the server.

The only difference with the socketio.Client.disconnect() method is that when the namespace argument is not given the namespace associated with the class is used.

emit(event, data=None, namespace=None, callback=None)

Emit a custom event to the server.

The only difference with the socketio.Client.emit() method is that when the namespace argument is not given the namespace associated with the class is used.

send(data, room=None, namespace=None, callback=None)

Send a message to the server.

The only difference with the socketio.Client.send() method is that when the namespace argument is not given the namespace associated with the class is used.

trigger_event(event, *args)

Dispatch an event to the proper handler method.

In the most common usage, this method is not overloaded by subclasses, as it performs the routing of events to methods. However, this method can be overridden if special dispatching rules are needed, or if having a single method that catches all events is desired.

class socketio.Namespace(namespace=None)

Base class for server-side class-based namespaces.

A class-based namespace is a class that contains all the event handlers for a Socket.IO namespace. The event handlers are methods of the class with the prefix on_, such as on_connect, on_disconnect,on_message, on_json, and so on.

Parameters:

namespace – The Socket.IO namespace to be used with all the event handlers defined in this class. If this argument is omitted, the default namespace is used.

call(event, data=None, to=None, sid=None, namespace=None, timeout=None, ignore_queue=False)

Emit a custom event to a client and wait for the response.

The only difference with the socketio.Server.call() method is that when the namespace argument is not given the namespace associated with the class is used.

close_room(room, namespace=None)

Close a room.

The only difference with the socketio.Server.close_room() method is that when the namespace argument is not given the namespace associated with the class is used.

disconnect(sid, namespace=None)

Disconnect a client.

The only difference with the socketio.Server.disconnect() method is that when the namespace argument is not given the namespace associated with the class is used.

emit(event, data=None, to=None, room=None, skip_sid=None, namespace=None, callback=None, ignore_queue=False)

Emit a custom event to one or more connected clients.

The only difference with the socketio.Server.emit() method is that when the namespace argument is not given the namespace associated with the class is used.

enter_room(sid, room, namespace=None)

Enter a room.

The only difference with the socketio.Server.enter_room() method is that when the namespace argument is not given the namespace associated with the class is used.

get_session(sid, namespace=None)

Return the user session for a client.

The only difference with the socketio.Server.get_session()method is that when the namespace argument is not given the namespace associated with the class is used.

leave_room(sid, room, namespace=None)

Leave a room.

The only difference with the socketio.Server.leave_room() method is that when the namespace argument is not given the namespace associated with the class is used.

rooms(sid, namespace=None)

Return the rooms a client is in.

The only difference with the socketio.Server.rooms() method is that when the namespace argument is not given the namespace associated with the class is used.

save_session(sid, session, namespace=None)

Store the user session for a client.

The only difference with the socketio.Server.save_session()method is that when the namespace argument is not given the namespace associated with the class is used.

send(data, to=None, room=None, skip_sid=None, namespace=None, callback=None, ignore_queue=False)

Send a message to one or more connected clients.

The only difference with the socketio.Server.send() method is that when the namespace argument is not given the namespace associated with the class is used.

session(sid, namespace=None)

Return the user session for a client with context manager syntax.

The only difference with the socketio.Server.session() method is that when the namespace argument is not given the namespace associated with the class is used.

trigger_event(event, *args)

Dispatch an event to the proper handler method.

In the most common usage, this method is not overloaded by subclasses, as it performs the routing of events to methods. However, this method can be overridden if special dispatching rules are needed, or if having a single method that catches all events is desired.

class socketio.AsyncClientNamespace(namespace=None)

Base class for asyncio client-side class-based namespaces.

A class-based namespace is a class that contains all the event handlers for a Socket.IO namespace. The event handlers are methods of the class with the prefix on_, such as on_connect, on_disconnect,on_message, on_json, and so on. These can be regular functions or coroutines.

Parameters:

namespace – The Socket.IO namespace to be used with all the event handlers defined in this class. If this argument is omitted, the default namespace is used.

async call(event, data=None, namespace=None, timeout=None)

Emit a custom event to the server and wait for the response.

The only difference with the socketio.Client.call() method is that when the namespace argument is not given the namespace associated with the class is used.

async disconnect()

Disconnect a client.

The only difference with the socketio.Client.disconnect() method is that when the namespace argument is not given the namespace associated with the class is used.

Note: this method is a coroutine.

async emit(event, data=None, namespace=None, callback=None)

Emit a custom event to the server.

The only difference with the socketio.Client.emit() method is that when the namespace argument is not given the namespace associated with the class is used.

Note: this method is a coroutine.

async send(data, namespace=None, callback=None)

Send a message to the server.

The only difference with the socketio.Client.send() method is that when the namespace argument is not given the namespace associated with the class is used.

Note: this method is a coroutine.

async trigger_event(event, *args)

Dispatch an event to the proper handler method.

In the most common usage, this method is not overloaded by subclasses, as it performs the routing of events to methods. However, this method can be overridden if special dispatching rules are needed, or if having a single method that catches all events is desired.

Note: this method is a coroutine.

class socketio.AsyncNamespace(namespace=None)

Base class for asyncio server-side class-based namespaces.

A class-based namespace is a class that contains all the event handlers for a Socket.IO namespace. The event handlers are methods of the class with the prefix on_, such as on_connect, on_disconnect,on_message, on_json, and so on. These can be regular functions or coroutines.

Parameters:

namespace – The Socket.IO namespace to be used with all the event handlers defined in this class. If this argument is omitted, the default namespace is used.

async call(event, data=None, to=None, sid=None, namespace=None, timeout=None, ignore_queue=False)

Emit a custom event to a client and wait for the response.

The only difference with the socketio.Server.call() method is that when the namespace argument is not given the namespace associated with the class is used.

async close_room(room, namespace=None)

Close a room.

The only difference with the socketio.Server.close_room() method is that when the namespace argument is not given the namespace associated with the class is used.

Note: this method is a coroutine.

async disconnect(sid, namespace=None)

Disconnect a client.

The only difference with the socketio.Server.disconnect() method is that when the namespace argument is not given the namespace associated with the class is used.

Note: this method is a coroutine.

async emit(event, data=None, to=None, room=None, skip_sid=None, namespace=None, callback=None, ignore_queue=False)

Emit a custom event to one or more connected clients.

The only difference with the socketio.Server.emit() method is that when the namespace argument is not given the namespace associated with the class is used.

Note: this method is a coroutine.

async enter_room(sid, room, namespace=None)

Enter a room.

The only difference with the socketio.Server.enter_room() method is that when the namespace argument is not given the namespace associated with the class is used.

Note: this method is a coroutine.

async get_session(sid, namespace=None)

Return the user session for a client.

The only difference with the socketio.Server.get_session()method is that when the namespace argument is not given the namespace associated with the class is used.

Note: this method is a coroutine.

async leave_room(sid, room, namespace=None)

Leave a room.

The only difference with the socketio.Server.leave_room() method is that when the namespace argument is not given the namespace associated with the class is used.

Note: this method is a coroutine.

rooms(sid, namespace=None)

Return the rooms a client is in.

The only difference with the socketio.Server.rooms() method is that when the namespace argument is not given the namespace associated with the class is used.

async save_session(sid, session, namespace=None)

Store the user session for a client.

The only difference with the socketio.Server.save_session()method is that when the namespace argument is not given the namespace associated with the class is used.

Note: this method is a coroutine.

async send(data, to=None, room=None, skip_sid=None, namespace=None, callback=None, ignore_queue=False)

Send a message to one or more connected clients.

The only difference with the socketio.Server.send() method is that when the namespace argument is not given the namespace associated with the class is used.

Note: this method is a coroutine.

session(sid, namespace=None)

Return the user session for a client with context manager syntax.

The only difference with the socketio.Server.session() method is that when the namespace argument is not given the namespace associated with the class is used.

async trigger_event(event, *args)

Dispatch an event to the proper handler method.

In the most common usage, this method is not overloaded by subclasses, as it performs the routing of events to methods. However, this method can be overridden if special dispatching rules are needed, or if having a single method that catches all events is desired.

Note: this method is a coroutine.

class socketio.Manager

Manage client connections.

This class keeps track of all the clients and the rooms they are in, to support the broadcasting of messages. The data used by this class is stored in a memory structure, making it appropriate only for single process services. More sophisticated storage backends can be implemented by subclasses.

close_room(room, namespace)

Remove all participants from a room.

connect(eio_sid, namespace)

Register a client connection to a namespace.

disconnect(sid, namespace, **kwargs)

Register a client disconnect from a namespace.

emit(event, data, namespace, room=None, skip_sid=None, callback=None, to=None, **kwargs)

Emit a message to a single client, a room, or all the clients connected to the namespace.

enter_room(sid, namespace, room, eio_sid=None)

Add a client to a room.

get_namespaces()

Return an iterable with the active namespace names.

get_participants(namespace, room)

Return an iterable with the active participants in a room.

get_rooms(sid, namespace)

Return the rooms a client is in.

initialize()

Invoked before the first request is received. Subclasses can add their initialization code here.

leave_room(sid, namespace, room)

Remove a client from a room.

pre_disconnect(sid, namespace)

Put the client in the to-be-disconnected list.

This allows the client data structures to be present while the disconnect handler is invoked, but still recognize the fact that the client is soon going away.

trigger_callback(sid, id, data)

Invoke an application callback.

class socketio.PubSubManager(channel='socketio', write_only=False, logger=None)

Manage a client list attached to a pub/sub backend.

This is a base class that enables multiple servers to share the list of clients, with the servers communicating events through a pub/sub backend. The use of a pub/sub backend also allows any client connected to the backend to emit events addressed to Socket.IO clients.

The actual backends must be implemented by subclasses, this class only provides a pub/sub generic framework.

Parameters:

channel – The channel name on which the server sends and receives notifications.

close_room(room, namespace=None)

Remove all participants from a room.

connect(eio_sid, namespace)

Register a client connection to a namespace.

disconnect(sid, namespace=None, **kwargs)

Register a client disconnect from a namespace.

emit(event, data, namespace=None, room=None, skip_sid=None, callback=None, to=None, **kwargs)

Emit a message to a single client, a room, or all the clients connected to the namespace.

This method takes care or propagating the message to all the servers that are connected through the message queue.

The parameters are the same as in Server.emit().

enter_room(sid, namespace, room, eio_sid=None)

Add a client to a room.

get_namespaces()

Return an iterable with the active namespace names.

get_participants(namespace, room)

Return an iterable with the active participants in a room.

get_rooms(sid, namespace)

Return the rooms a client is in.

initialize()

Invoked before the first request is received. Subclasses can add their initialization code here.

leave_room(sid, namespace, room)

Remove a client from a room.

pre_disconnect(sid, namespace)

Put the client in the to-be-disconnected list.

This allows the client data structures to be present while the disconnect handler is invoked, but still recognize the fact that the client is soon going away.

trigger_callback(sid, id, data)

Invoke an application callback.

class socketio.KombuManager(url='amqp://guest:guest@localhost:5672//', channel='socketio', write_only=False, logger=None, connection_options=None, exchange_options=None, queue_options=None, producer_options=None)

Client manager that uses kombu for inter-process messaging.

This class implements a client manager backend for event sharing across multiple processes, using RabbitMQ, Redis or any other messaging mechanism supported by kombu.

To use a kombu backend, initialize the Server instance as follows:

url = 'amqp://user:password@hostname:port//' server = socketio.Server(client_manager=socketio.KombuManager(url))

Parameters:

close_room(room, namespace=None)

Remove all participants from a room.

connect(eio_sid, namespace)

Register a client connection to a namespace.

disconnect(sid, namespace=None, **kwargs)

Register a client disconnect from a namespace.

emit(event, data, namespace=None, room=None, skip_sid=None, callback=None, to=None, **kwargs)

Emit a message to a single client, a room, or all the clients connected to the namespace.

This method takes care or propagating the message to all the servers that are connected through the message queue.

The parameters are the same as in Server.emit().

enter_room(sid, namespace, room, eio_sid=None)

Add a client to a room.

get_namespaces()

Return an iterable with the active namespace names.

get_participants(namespace, room)

Return an iterable with the active participants in a room.

get_rooms(sid, namespace)

Return the rooms a client is in.

initialize()

Invoked before the first request is received. Subclasses can add their initialization code here.

leave_room(sid, namespace, room)

Remove a client from a room.

pre_disconnect(sid, namespace)

Put the client in the to-be-disconnected list.

This allows the client data structures to be present while the disconnect handler is invoked, but still recognize the fact that the client is soon going away.

trigger_callback(sid, id, data)

Invoke an application callback.

class socketio.RedisManager(url='redis://localhost:6379/0', channel='socketio', write_only=False, logger=None, redis_options=None)

Redis based client manager.

This class implements a Redis backend for event sharing across multiple processes. Only kept here as one more example of how to build a custom backend, since the kombu backend is perfectly adequate to support a Redis message queue.

To use a Redis backend, initialize the Server instance as follows:

url = 'redis://hostname:port/0' server = socketio.Server(client_manager=socketio.RedisManager(url))

Parameters:

close_room(room, namespace=None)

Remove all participants from a room.

connect(eio_sid, namespace)

Register a client connection to a namespace.

disconnect(sid, namespace=None, **kwargs)

Register a client disconnect from a namespace.

emit(event, data, namespace=None, room=None, skip_sid=None, callback=None, to=None, **kwargs)

Emit a message to a single client, a room, or all the clients connected to the namespace.

This method takes care or propagating the message to all the servers that are connected through the message queue.

The parameters are the same as in Server.emit().

enter_room(sid, namespace, room, eio_sid=None)

Add a client to a room.

get_namespaces()

Return an iterable with the active namespace names.

get_participants(namespace, room)

Return an iterable with the active participants in a room.

get_rooms(sid, namespace)

Return the rooms a client is in.

initialize()

Invoked before the first request is received. Subclasses can add their initialization code here.

leave_room(sid, namespace, room)

Remove a client from a room.

pre_disconnect(sid, namespace)

Put the client in the to-be-disconnected list.

This allows the client data structures to be present while the disconnect handler is invoked, but still recognize the fact that the client is soon going away.

trigger_callback(sid, id, data)

Invoke an application callback.

class socketio.KafkaManager(url='kafka://localhost:9092', channel='socketio', write_only=False)

Kafka based client manager.

This class implements a Kafka backend for event sharing across multiple processes.

To use a Kafka backend, initialize the Server instance as follows:

url = 'kafka://hostname:port' server = socketio.Server(client_manager=socketio.KafkaManager(url))

Parameters:

close_room(room, namespace=None)

Remove all participants from a room.

connect(eio_sid, namespace)

Register a client connection to a namespace.

disconnect(sid, namespace=None, **kwargs)

Register a client disconnect from a namespace.

emit(event, data, namespace=None, room=None, skip_sid=None, callback=None, to=None, **kwargs)

Emit a message to a single client, a room, or all the clients connected to the namespace.

This method takes care or propagating the message to all the servers that are connected through the message queue.

The parameters are the same as in Server.emit().

enter_room(sid, namespace, room, eio_sid=None)

Add a client to a room.

get_namespaces()

Return an iterable with the active namespace names.

get_participants(namespace, room)

Return an iterable with the active participants in a room.

get_rooms(sid, namespace)

Return the rooms a client is in.

initialize()

Invoked before the first request is received. Subclasses can add their initialization code here.

leave_room(sid, namespace, room)

Remove a client from a room.

pre_disconnect(sid, namespace)

Put the client in the to-be-disconnected list.

This allows the client data structures to be present while the disconnect handler is invoked, but still recognize the fact that the client is soon going away.

trigger_callback(sid, id, data)

Invoke an application callback.

class socketio.AsyncManager

Manage a client list for an asyncio server.

async close_room(room, namespace)

Remove all participants from a room.

Note: this method is a coroutine.

async connect(eio_sid, namespace)

Register a client connection to a namespace.

Note: this method is a coroutine.

async disconnect(sid, namespace, **kwargs)

Disconnect a client.

Note: this method is a coroutine.

async emit(event, data, namespace, room=None, skip_sid=None, callback=None, to=None, **kwargs)

Emit a message to a single client, a room, or all the clients connected to the namespace.

Note: this method is a coroutine.

async enter_room(sid, namespace, room, eio_sid=None)

Add a client to a room.

Note: this method is a coroutine.

get_namespaces()

Return an iterable with the active namespace names.

get_participants(namespace, room)

Return an iterable with the active participants in a room.

get_rooms(sid, namespace)

Return the rooms a client is in.

initialize()

Invoked before the first request is received. Subclasses can add their initialization code here.

async leave_room(sid, namespace, room)

Remove a client from a room.

Note: this method is a coroutine.

pre_disconnect(sid, namespace)

Put the client in the to-be-disconnected list.

This allows the client data structures to be present while the disconnect handler is invoked, but still recognize the fact that the client is soon going away.

async trigger_callback(sid, id, data)

Invoke an application callback.

Note: this method is a coroutine.

class socketio.AsyncRedisManager(url='redis://localhost:6379/0', channel='socketio', write_only=False, logger=None, redis_options=None)

Redis based client manager for asyncio servers.

This class implements a Redis backend for event sharing across multiple processes.

To use a Redis backend, initialize the AsyncServer instance as follows:

url = 'redis://hostname:port/0' server = socketio.AsyncServer( client_manager=socketio.AsyncRedisManager(url))

Parameters:

async close_room(room, namespace=None)

Remove all participants from a room.

Note: this method is a coroutine.

async connect(eio_sid, namespace)

Register a client connection to a namespace.

Note: this method is a coroutine.

async disconnect(sid, namespace, **kwargs)

Disconnect a client.

Note: this method is a coroutine.

async emit(event, data, namespace=None, room=None, skip_sid=None, callback=None, to=None, **kwargs)

Emit a message to a single client, a room, or all the clients connected to the namespace.

This method takes care or propagating the message to all the servers that are connected through the message queue.

The parameters are the same as in Server.emit().

Note: this method is a coroutine.

async enter_room(sid, namespace, room, eio_sid=None)

Add a client to a room.

Note: this method is a coroutine.

get_namespaces()

Return an iterable with the active namespace names.

get_participants(namespace, room)

Return an iterable with the active participants in a room.

get_rooms(sid, namespace)

Return the rooms a client is in.

initialize()

Invoked before the first request is received. Subclasses can add their initialization code here.

async leave_room(sid, namespace, room)

Remove a client from a room.

Note: this method is a coroutine.

pre_disconnect(sid, namespace)

Put the client in the to-be-disconnected list.

This allows the client data structures to be present while the disconnect handler is invoked, but still recognize the fact that the client is soon going away.

async trigger_callback(sid, id, data)

Invoke an application callback.

Note: this method is a coroutine.

class socketio.AsyncAioPikaManager(url='amqp://guest:guest@localhost:5672//', channel='socketio', write_only=False, logger=None)

Client manager that uses aio_pika for inter-process messaging under asyncio.

This class implements a client manager backend for event sharing across multiple processes, using RabbitMQ

To use a aio_pika backend, initialize the Server instance as follows:

url = 'amqp://user:password@hostname:port//' server = socketio.Server(client_manager=socketio.AsyncAioPikaManager( url))

Parameters:

async close_room(room, namespace=None)

Remove all participants from a room.

Note: this method is a coroutine.

async connect(eio_sid, namespace)

Register a client connection to a namespace.

Note: this method is a coroutine.

async disconnect(sid, namespace, **kwargs)

Disconnect a client.

Note: this method is a coroutine.

async emit(event, data, namespace=None, room=None, skip_sid=None, callback=None, to=None, **kwargs)

Emit a message to a single client, a room, or all the clients connected to the namespace.

This method takes care or propagating the message to all the servers that are connected through the message queue.

The parameters are the same as in Server.emit().

Note: this method is a coroutine.

async enter_room(sid, namespace, room, eio_sid=None)

Add a client to a room.

Note: this method is a coroutine.

get_namespaces()

Return an iterable with the active namespace names.

get_participants(namespace, room)

Return an iterable with the active participants in a room.

get_rooms(sid, namespace)

Return the rooms a client is in.

initialize()

Invoked before the first request is received. Subclasses can add their initialization code here.

async leave_room(sid, namespace, room)

Remove a client from a room.

Note: this method is a coroutine.

pre_disconnect(sid, namespace)

Put the client in the to-be-disconnected list.

This allows the client data structures to be present while the disconnect handler is invoked, but still recognize the fact that the client is soon going away.

async trigger_callback(sid, id, data)

Invoke an application callback.

Note: this method is a coroutine.