Simple Clients

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:
  • event – The event name. It can be any string. The event names 'connect', 'message' and 'disconnect' are reserved and should not be used.

  • data – The data to send to the server. Data can be of type str, bytes, list or dict. To send multiple arguments, use a tuple where each element is of one of the types indicated above.

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

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:
  • url – The URL of the Socket.IO server. It can include custom query string parameters if required by the server. If a function is provided, the client will invoke it to obtain the URL each time a connection or reconnection is attempted.

  • headers – A dictionary with custom headers to send with the connection request. If a function is provided, the client will invoke it to obtain the headers dictionary each time a connection or reconnection is attempted.

  • auth – Authentication data passed to the server with the connection request, normally a dictionary with one or more string key/value pairs. If a function is provided, the client will invoke it to obtain the authentication data each time a connection or reconnection is attempted.

  • transports – The list of allowed transports. Valid transports are 'polling' and 'websocket'. If not given, the polling transport is connected first, then an upgrade to websocket is attempted.

  • namespace – The namespace to connect to as a string. If not given, the default namespace / is used.

  • socketio_path – The endpoint where the Socket.IO server is installed. The default value is appropriate for most cases.

  • wait_timeout – How long the client should wait for the connection to be established. The default is 5 seconds.

disconnect()

Disconnect from the server.

emit(event, data=None)

Emit an event to the server.

Parameters:
  • event – The event name. It can be any string. The event names 'connect', 'message' and 'disconnect' are reserved and should not be used.

  • data – The data to send to the server. Data can be of type str, bytes, list or dict. To send multiple arguments, use a tuple where each element is of one of the types indicated above.

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 a TimeoutError 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 polling and 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:
  • event – The event name. It can be any string. The event names 'connect', 'message' and 'disconnect' are reserved and should not be used.

  • data – The data to send to the server. Data can be of type str, bytes, list or dict. To send multiple arguments, use a tuple where each element is of one of the types indicated above.

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

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:
  • url – The URL of the Socket.IO server. It can include custom query string parameters if required by the server. If a function is provided, the client will invoke it to obtain the URL each time a connection or reconnection is attempted.

  • headers – A dictionary with custom headers to send with the connection request. If a function is provided, the client will invoke it to obtain the headers dictionary each time a connection or reconnection is attempted.

  • auth – Authentication data passed to the server with the connection request, normally a dictionary with one or more string key/value pairs. If a function is provided, the client will invoke it to obtain the authentication data each time a connection or reconnection is attempted.

  • transports – The list of allowed transports. Valid transports are 'polling' and 'websocket'. If not given, the polling transport is connected first, then an upgrade to websocket is attempted.

  • namespace – The namespace to connect to as a string. If not given, the default namespace / is used.

  • socketio_path – The endpoint where the Socket.IO server is installed. The default value is appropriate for most cases.

  • wait_timeout – How long the client should wait for the connection. The default is 5 seconds.

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:
  • event – The event name. It can be any string. The event names 'connect', 'message' and 'disconnect' are reserved and should not be used.

  • data – The data to send to the server. Data can be of type str, bytes, list or dict. To send multiple arguments, use a tuple where each element is of one of the types indicated above.

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 a TimeoutError 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 polling and websocket.