Skip to main content

Protocol & Messages

aiosignalr.protocol

IHubProtocol

Interface implemented by JSONProtocol and MessagePackProtocol.

  • name"json" / "messagepack".
  • version — protocol version (2).
  • transfer_formatTransferFormat.TEXT / BINARY.
  • write_message(message) -> bytes — serialize one message into a frame.
  • feed(data) -> list[HubMessage | None] — parse complete frames from a byte stream.
  • create_parser() -> IHubProtocol — fresh connection-bound instance.

JSONProtocol

Text protocol. Each message is one JSON object terminated by 0x1E.

MessagePackProtocol

Binary protocol. Each message is a MessagePack array prefixed by a VarInt length.

Handshake helpers

  • encode_handshake_request(protocol, version) -> bytes
  • encode_handshake_response(error=None) -> bytes
  • HandshakeParser — incremental, record-separator-delimited parser.
  • parse_handshake(frame)HandshakeRequestMessage | HandshakeResponseMessage.

aiosignalr.messages

Immutable (frozen, slots) data classes for every hub message:

Classmessage_typeFields
InvocationMessageINVOCATIONtarget, arguments, invocation_id, stream_ids, headers
StreamInvocationMessageSTREAM_INVOCATIONtarget, arguments, invocation_id, stream_ids, headers
StreamItemMessageSTREAM_ITEMinvocation_id, item
CompletionMessageCOMPLETIONinvocation_id, error, result, has_result
CancelInvocationMessageCANCEL_INVOCATIONinvocation_id
PingMessagePING
CloseMessageCLOSEerror, allow_reconnect
AckMessageACKsequence_id
SequenceMessageSEQUENCEsequence_id
HandshakeRequestMessageprotocol, version
HandshakeResponseMessageerror

aiosignalr.message_buffer.MessageBuffer

The shared stateful-reconnect buffer (see Stateful Reconnect). Used by both client and server.

MessageBuffer(
protocol: IHubProtocol,
write: Callable[[bytes], Awaitable[None]],
*,
buffer_size: int = 100_000,
ack_interval: float = 1.0,
)
MethodDescription
async send(message, data)Buffer a trackable message and write it
async send_serialized(data)Buffer a pre-encoded frame (broadcast fanout)
should_process(message) -> boolDedupe / gate inbound messages
ack(message)Drop buffered messages ≤ ack sequence id
disconnected()Enter reconnect state (ignore non-sequence messages)
async resend()Send Sequence + replay the buffer
dispose()Cancel the pending ack timer

aiosignalr.framing

  • encode_text_message(data) -> bytes — append 0x1E.
  • TextMessageParser(max_size) — incremental 0x1E-delimited parser.
  • encode_binary_message(payload) -> bytes — VarInt length prefix.
  • BinaryMessageParser(max_size) — incremental length-prefixed parser.