TCP v2 framing¶
The Server API supports two wire formats on the same TCP port. The format is chosen once per connection, by the first line the client sends.
| Format | Selected when | Message boundary |
|---|---|---|
| Line-based (v1) | The first line is a JSON request | Each JSON message ends with \r\n |
| TCP v2 | The first line is exactly ION2 |
Each JSON message is a length-prefixed frame |
TCP v2 changes only how messages are delimited. Commands, the command / data / extID / __token request envelope, the extID / status / data response envelope, events, and permissions are identical in both formats.
Length-prefixed frames let the client know the size of a message before it is complete, so large responses are received without scanning the stream for a delimiter, and a truncated or corrupted message is detected instead of being glued to the next one.
Connection sequence¶
- The client opens the TCP connection.
- The client sends the preface: the six bytes
ION2\r\n. - The server answers with one
HELLO_ACKframe. - From this point both sides exchange
JSON_DATAframes only.
The client must not send any request before it has received and validated HELLO_ACK.
The preface is recognized only as the first line of a connection. A connection that started in the line-based format cannot be switched to TCP v2, and a TCP v2 connection cannot fall back to lines. To change the format, open a new connection.
Frame layout¶
Every frame has a fixed 16-byte header followed by the payload. Multi-byte integers are unsigned and big-endian.
| Offset | Size | Field | Value |
|---|---|---|---|
| 0 | 4 | Magic | ASCII ION2 |
| 4 | 1 | Version | 2 |
| 5 | 1 | Frame type | 1 = HELLO_ACK, 2 = JSON_DATA |
| 6 | 2 | Flags | 0 (no flags are defined) |
| 8 | 4 | Payload length | Number of payload bytes that follow the header |
| 12 | 4 | Decoded length | Size of the payload after decoding; equal to the payload length while compression is none |
| 16 | N | Payload | One JSON value encoded as UTF-8 |
The payload is never empty and is not terminated by \r\n.
Example: the frame that carries the JSON value {}.
49 4F 4E 32 02 02 00 00 00 00 00 02 00 00 00 02 7B 7D
I O N 2 ver type flags length = 2 decoded = 2 { }
HELLO_ACK¶
HELLO_ACK is sent once by the server, in reply to the preface. Its payload describes the negotiated parameters:
{
"protocol": 2,
"encoding": "json",
"compression": "none",
"max_request_bytes": 1048576,
"max_response_bytes": 134217712,
"capabilities": ["json_frames"]
}
| Field | Type | Meaning |
|---|---|---|
protocol |
int | Wire format version, always 2 |
encoding |
string | Payload encoding, always json |
compression |
string | Payload compression, always none |
max_request_bytes |
int | Largest request payload the server accepts |
max_response_bytes |
int | Largest payload the server can send in one frame |
capabilities |
string[] | Supported features; json_frames is always present |
A client should verify protocol, encoding, compression, and the presence of json_frames, and ignore capability names it does not know.
JSON_DATA¶
After the handshake every message in both directions is a JSON_DATA frame with exactly one JSON value.
Request:
{"command":"MngGetAccounts","data":{},"extID":"unique-id","__token":"jwt"}
Response:
{"extID":"unique-id","status":200,"data":{}}
Server events and quotes arrive on the same connection as separate JSON_DATA frames, interleaved with responses. Match a response to its request by extID; do not rely on the order of frames.
Limits¶
| Limit | Value |
|---|---|
| Request payload | 1,048,576 bytes (1 MiB) |
| Response or event payload | 134,217,712 bytes (128 MiB minus the 16-byte header) |
| Server outbound queue per connection | 128 MiB and 4096 messages |
| Server write timeout | 30 seconds |
The response timeout and slow-connection rules described in Timeouts And Large Responses apply to TCP v2 unchanged.
Protocol errors¶
The server closes the connection without a response when a TCP v2 client sends:
- a header with a wrong magic, version, frame type, or non-zero flags;
- a payload length of zero or above the request limit;
- a decoded length that differs from the payload length;
- a frame type other than
JSON_DATA; - a payload that contains a NUL byte.
A payload that is framed correctly but is not a valid JSON object is not a protocol error. It is answered as a regular request with status 400 and INVALID_JSON_OBJECT, and the connection stays open.
After a disconnect the client must repeat the whole sequence, starting with the preface. Requests that were not answered before the disconnect are not replayed by the server.
Servers without TCP v2¶
A server that does not support TCP v2 reads the preface as an ordinary request line and answers in the line-based format:
{"extID":"","status":400,"data":{"error":"INVALID_JSON_OBJECT"}}
A client that receives a line starting with { instead of a frame starting with ION2 should treat the server as not supporting TCP v2. The Back Office application does not fall back to the line-based format in this case; it reports that the server must be updated.
Minimal client¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
The example keeps the receive logic short. A production client should also bound the receive buffer by max_response_bytes, avoid re-copying the buffer on every chunk, and apply its own handshake timeout.