Skip to main content

Client API

aiosignalr.client

HubConnection

The client connection. See the client guide for usage.

Construction

HubConnection(
*,
protocol: str | IHubProtocol = "json",
options: HubConnectionOptions | None = None,
logger: logging.Logger | None = None,
)

Builder methods

MethodDescription
with_retry_policy(policy) -> HubConnectionEnable automatic reconnect
with_stateful_reconnect(buffer_size=100_000) -> HubConnectionEnable stateful reconnect

Connection methods

MethodDescription
async start(url, *, access_token_factory=None, headers=None, transports=None, skip_negotiation=False)Negotiate, connect, handshake
async stop()Gracefully close and clean up
async invoke(target, *args) -> AnyCall and await the result
async stream(target, *args) -> AsyncIteratorCall a streaming method
async send(target, *args)Fire-and-forget call
async client_stream(target, *args) -> AsyncIterator[_UploadHandle]Upload a client stream
on(target, handler) -> HubConnectionRegister a server-call handler
remove(target, handler)Remove a handler

Properties & events

MemberTypeDescription
stateConnectionStateCurrent lifecycle state
on_openCallable[[], ...] | NoneFired on first connect
on_closeCallable[[BaseException | None], ...] | NoneFired on permanent close
on_reconnectingCallable[[BaseException | None], ...] | NoneFired when a reconnect cycle starts
on_reconnectedCallable[[], ...] | NoneFired when a reconnect succeeds

_UploadHandle

Returned by client_stream():

  • async send_item(item) — upload one item.
  • async complete(error=None) — finish the upload.
  • result — the server result after completion.

HubConnectionOptions

FieldDefaultDescription
handshake_timeout15.0Seconds to wait for the handshake
server_timeout30.0Seconds of silence before the client drops
keep_alive_interval15.0Seconds between client pings
stateful_reconnect_buffer_size100_000Outbound buffer byte limit

Retry policies

RetryContext(previous_retry_count, elapsed_time, retry_reason) — passed to RetryPolicy.next_retry_delay.

  • RetryPolicy (ABC) — implement next_retry_delay(context) -> float | None.
  • DefaultRetryPolicy0s, 2s, 10s, 30s, then None.

Negotiation helpers

negotiate(session, base_url, *, headers=None, negotiate_version=1, use_stateful_reconnect=False) -> NegotiateResult

NegotiateResult fields:

FieldDescription
connection_idServer connection id
connection_tokenToken used in the ?id= query (negotiate v1+)
negotiate_versionAgreed negotiate version
available_transports[AvailableTransport(name, transfer_formats)]
use_stateful_reconnectWhether the ack protocol is agreed
url / access_tokenRedirect response fields (if any)

Constants: WEB_SOCKETS, SERVER_SENT_EVENTS, LONG_POLLING, DEFAULT_TRANSPORTS, NEGOTIATE_VERSION.

aiosignalr.enums

EnumValues
ConnectionStateDISCONNECTED, CONNECTING, CONNECTED, RECONNECTING
TransferFormatTEXT, BINARY
MessageTypeINVOCATION=1 … SEQUENCE=9