appyter.ext.socketio package

Submodules

appyter.ext.socketio.chunked_emit module

class appyter.ext.socketio.chunked_emit.ChunkedEmitMixin[source]

Bases: object

A mixin for splitting up large emit calls into chunked calls of lower priority

CHUNK_SIZE = 1048576
async emit(evt, data, priority=0, **kwargs)[source]

appyter.ext.socketio.client module

class appyter.ext.socketio.client.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)[source]

Bases: ChunkedEmitMixin, ForwardingMixin, PriorityQueuedEmitMixin, AsyncClient

Client with packet forwarding, emit buffering & chunking async with is necessary for initialization

async connect(*args, headers={}, **kwargs)[source]

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.

  • namespaces – The namespaces to connect as a string or list of strings. If not given, the namespaces that have registered event handlers are connected.

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

  • wait – if set to True (the default) the call only returns when all the namespaces are connected. If set to False, the call returns as soon as the Engine.IO transport is connected, and the namespaces will connect in the background.

  • wait_timeout – How long the client should wait for the connection. The default is 1 second. This argument is only considered when wait is set to True.

  • retry – Apply the reconnection logic if the initial connection attempt fails. The default is False.

Note: this method is a coroutine.

Example usage:

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

appyter.ext.socketio.forwarding module

class appyter.ext.socketio.forwarding.ForwardingMixin[source]

Bases: object

A mixin for converting to= messages on the client to forward events to be handled by the server

async emit(evt, data, to=None, priority=0, **kwargs)[source]

appyter.ext.socketio.priority_queued_emit module

class appyter.ext.socketio.priority_queued_emit.PriorityQueuedEmitMixin[source]

Bases: object

A mixin for queuing emit calls to get triggered sequentially when emit_enabled event is set

async disconnect()[source]
async emit(evt, data, priority=0, **kwargs)[source]

appyter.ext.socketio.server module

class appyter.ext.socketio.server.AsyncServer(*args, **kwargs)[source]

Bases: ChunkedEmitMixin, PriorityQueuedEmitMixin, AsyncServer

Server with packet forwarding. async with is necessary for initialization

async forward(sid, data)[source]
on(event)[source]

Register an event handler.

Parameters:
  • event – The event name. It can be any string. The event names 'connect', 'message' and 'disconnect' are reserved and should not be used. The '*' event name can be used to define a catch-all event handler.

  • handler – The function that should be invoked to handle the event. When this parameter is not given, the method acts as a decorator for the handler function.

  • namespace – The Socket.IO namespace for the event. If this argument is omitted the handler is associated with the default namespace. A catch-all namespace can be defined by passing '*' as the namespace.

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:

  • The 'connect' event handler receives the sid (session ID) for the client and the WSGI environment dictionary as arguments.

  • The 'disconnect' handler receives the sid for the client as only argument.

  • The 'message' handler and handlers for custom event names receive the sid for the client and the message payload as arguments. Any values returned from a message handler will be passed to the client’s acknowledgement callback function if it exists.

  • A catch-all event handler receives the event name as first argument, followed by any arguments specific to the event.

  • A catch-all namespace event handler receives the namespace as first argument, followed by any arguments specific to the event.

  • A combined catch-all namespace and catch-all event handler receives the event name as first argument and the namespace as second argument, followed by any arguments specific to the event.

Module contents