Skip to main content

JSON Protocol

The JSON protocol is the default hub protocol. Every message is a single JSON object terminated by the ASCII record separator 0x1E.

Message types

typeMessageDirection
1Invocationboth
2StreamItemboth
3Completionboth
4StreamInvocationboth
5CancelInvocationboth
6Pingboth
7Closeboth
8Ackboth (stateful reconnect)
9Sequenceboth (stateful reconnect)

Frame shape

Each message is encoded as one JSON object followed by 0x1E:

{"type":1,"target":"Add","arguments":[40,2],"invocationId":"1"}\x1e

Selection

connection = HubConnection(protocol="json") # default

The handshake declares the protocol:

{"protocol": "json", "version": 2}\x1e

Version 2 is sent when stateful reconnect is negotiated; version 1 otherwise, so older servers keep working.

Example wire messages

Invocation:

{"type":1,"invocationId":"1","target":"Add","arguments":[40,2]}

StreamItem:

{"type":2,"invocationId":"1","item":0}

Completion with result:

{"type":3,"invocationId":"1","result":42}

Ack:

{"type":8,"sequenceId":7}

Sequence:

{"type":9,"sequenceId":8}

Parser behavior

  • Unknown message types are ignored (forward compatibility).
  • Missing required properties raise a ProtocolError, which is fatal for the connection.
  • The error and result properties of a Completion are mutually exclusive.
  • 0x1E-delimited parsing is incremental and size-limited (maximum_receive_message_size, default 32 KiB).

See also