Skip to content

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

  1. The client opens the TCP connection.
  2. The client sends the preface: the six bytes ION2\r\n.
  3. The server answers with one HELLO_ACK frame.
  4. From this point both sides exchange JSON_DATA frames 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
const net = require("net");

const HEADER_BYTES = 16;
const HELLO_ACK = 1;
const JSON_DATA = 2;

function encodeFrame(value) {
  const payload = Buffer.from(JSON.stringify(value), "utf8");
  const header = Buffer.alloc(HEADER_BYTES);
  header.write("ION2", 0, "ascii");
  header[4] = 2;                            // version
  header[5] = JSON_DATA;                    // frame type
  header.writeUInt32BE(payload.length, 8);  // payload length
  header.writeUInt32BE(payload.length, 12); // decoded length
  return Buffer.concat([header, payload]);
}

const socket = net.connect({ host: "example.host", port: 8080 });
let received = Buffer.alloc(0);
let ready = false;

socket.on("connect", () => socket.write("ION2\r\n"));

socket.on("data", (chunk) => {
  received = Buffer.concat([received, chunk]);
  while (received.length >= HEADER_BYTES) {
    if (received.toString("ascii", 0, 4) !== "ION2" || received[4] !== 2) {
      socket.destroy(new Error("Server does not support TCP v2"));
      return;
    }
    const length = received.readUInt32BE(8);
    if (received.length < HEADER_BYTES + length) return; // wait for the rest of the frame
    const type = received[5];
    const message = JSON.parse(received.toString("utf8", HEADER_BYTES, HEADER_BYTES + length));
    received = received.subarray(HEADER_BYTES + length);

    if (!ready) {
      if (type !== HELLO_ACK || message.protocol !== 2) {
        socket.destroy(new Error("Unexpected handshake response"));
        return;
      }
      ready = true;
      socket.write(encodeFrame({
        command: "Subscribe",
        data: { chanels: ["EURUSD"] },
        extID: "sub1",
        __token: "your-jwt-token",
      }));
    } else {
      console.log(message); // response (has extID) or event
    }
  }
});

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.