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
| Method | Description |
|---|---|
with_retry_policy(policy) -> HubConnection | Enable automatic reconnect |
with_stateful_reconnect(buffer_size=100_000) -> HubConnection | Enable stateful reconnect |
Connection methods
| Method | Description |
|---|---|
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) -> Any | Call and await the result |
async stream(target, *args) -> AsyncIterator | Call 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) -> HubConnection | Register a server-call handler |
remove(target, handler) | Remove a handler |
Properties & events
| Member | Type | Description |
|---|---|---|
state | ConnectionState | Current lifecycle state |
on_open | Callable[[], ...] | None | Fired on first connect |
on_close | Callable[[BaseException | None], ...] | None | Fired on permanent close |
on_reconnecting | Callable[[BaseException | None], ...] | None | Fired when a reconnect cycle starts |
on_reconnected | Callable[[], ...] | None | Fired 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
| Field | Default | Description |
|---|---|---|
handshake_timeout | 15.0 | Seconds to wait for the handshake |
server_timeout | 30.0 | Seconds of silence before the client drops |
keep_alive_interval | 15.0 | Seconds between client pings |
stateful_reconnect_buffer_size | 100_000 | Outbound buffer byte limit |
Retry policies
RetryContext(previous_retry_count, elapsed_time, retry_reason) — passed to
RetryPolicy.next_retry_delay.
RetryPolicy(ABC) — implementnext_retry_delay(context) -> float | None.DefaultRetryPolicy—0s, 2s, 10s, 30s, thenNone.
Negotiation helpers
negotiate(session, base_url, *, headers=None, negotiate_version=1, use_stateful_reconnect=False) -> NegotiateResult
NegotiateResult fields:
| Field | Description |
|---|---|
connection_id | Server connection id |
connection_token | Token used in the ?id= query (negotiate v1+) |
negotiate_version | Agreed negotiate version |
available_transports | [AvailableTransport(name, transfer_formats)] |
use_stateful_reconnect | Whether the ack protocol is agreed |
url / access_token | Redirect response fields (if any) |
Constants: WEB_SOCKETS, SERVER_SENT_EVENTS, LONG_POLLING,
DEFAULT_TRANSPORTS, NEGOTIATE_VERSION.
aiosignalr.enums
| Enum | Values |
|---|---|
ConnectionState | DISCONNECTED, CONNECTING, CONNECTED, RECONNECTING |
TransferFormat | TEXT, BINARY |
MessageType | INVOCATION=1 … SEQUENCE=9 |