Skip to main content

Server API

aiosignalr.server

SignalRServer

The hub server. See the server guide for usage.

SignalRServer(
hub: type[Hub],
*,
options: ServerOptions | None = None,
logger: logging.Logger | None = None,
)

Methods & attributes

MemberDescription
protocol_registry{"json": JSONProtocol, "messagepack": MessagePackProtocol}
lifetimeHubLifetimeManager used for broadcasts/groups
dispatcherHubDispatcher that binds hub methods
async serve(host, port, *, path="/hub") -> tuple[str, int]Run standalone aiohttp server
create_application(*, path="/hub")Build the aiohttp app
asgi_app(*, path="/hub")Build an ASGI app for uvicorn/FastAPI
async close()Shut down the standalone server
get_connection(connection_id)Look up a live connection context

ServerOptions

FieldDefaultDescription
keep_alive_interval15.0Server ping interval
client_timeout_interval30.0Silent-client timeout
handshake_timeout15.0Handshake deadline
maximum_parallel_invocations1Concurrent invocations per connection
maximum_receive_message_size32768Max inbound frame size
stream_buffer_capacity10Upload-stream queue capacity
long_polling_timeout15.0Long poll duration
enable_detailed_errorsFalseSend exception details to clients
allow_stateful_reconnectsFalseEnable stateful reconnect
stateful_reconnect_buffer_size100_000Outbound buffer bytes
stateful_reconnect_timeout30.0Seconds a dropped connection is kept

Hub

Base class for hubs. Public methods become invocable; async generators become streaming methods.

  • self.clientsHubClients proxy (see below).
  • self.groupsGroupsManager for the caller's membership.
  • self.contextHubContext with connection_id, headers, query, user_id.
  • async on_connected() — per-connection hook.
  • async on_disconnected(exception) — per-connection hook.

HubClients

MemberDescription
all_Proxy for every client (send(method, *args))
othersEvery client except the caller
connection(id)A single connection
connections(ids)Several connections
group(name)Members of a group
groups(names)Members of several groups
user(id)Connections with a user id
users(ids)Connections with several user ids
callerThe calling client (invoke(...) supports client results)

HubLifetimeManager / DefaultHubLifetimeManager

Abstract registry used for broadcasts and groups. DefaultHubLifetimeManager is the in-memory implementation: it tracks connections, maintains groups and serializes each broadcast once per protocol. Subclass it to plug in a distributed backplane.

HubConnectionContext

Per-connection server state (connection id, negotiated protocol, outbound transport, message buffer, invocation tracking). Normally you interact with it only via self.context in hub methods or custom lifetime/dispatcher code.

  • has_transport — whether an outbound transport is attached.
  • connection_id — logical connection id.
  • use_stateful_reconnect — whether the ack protocol is active.
  • async send(message) / send_bytes(data) / send_serialized(data) — outbound writes (the buffer-aware path).
  • wait_closed() — resolves when the connection closes.

IncomingStream

Async-iterable bound to an upload-stream parameter (async for item in stream).

StreamTracker

Tracks client-to-server upload streams for a connection.

HubDispatcher

Discovers hub methods and routes messages. See method resolution rules.

asgi_app

asgi_app(server, *, path="/hub") — returns a plain ASGI app; also available as server.asgi_app().