The TradeStation API offers HTTP Streaming responses for some specialized resources including intraday barcharts, quote changes, and quote snapshots. These streams conform to RFC2616 for HTTP/1.1 Streaming with some slight modifications.
The HTTP streaming mechanism keeps a request open indefinitely. It never terminates the request or closes the connection, even after the server pushes data to the client. This mechanism significantly reduces the network latency because the client and the server do not need to open and close the connection.
The basic life cycle of an application using HTTP streaming is as follows:
The client makes an initial request and then waits for a response.
The server defers the response to a poll request until an update is available, or until a particular status or timeout has occurred.
Whenever an update is available, the server sends it back to the client as a part of the response.
The data sent by the server does not terminate the request or the connection. The server returns to step 3.
The HTTP streaming mechanism is based on the capability of the server to send several pieces of information in the same response, without terminating the request or the connection.
Source: RFC6202, Page 7.
HTTP Streaming resources are identified under in this documentation as such, all other resources conform to the HTTP Request pattern instead.
The HTTP Streaming response is returned with the following headers:
Content-Length header is typically omitted since the response
body size is unknown.
Streams consist of a series of chunks that contain individual JSON objects to be parsed separately rather than as a whole response body.
One unique thing about TradeStation's HTTP Streams is they also can terminate unlike a canonical HTTP/1.1 Stream.
In the case of
ERROR, it will often be followed by an error message like:
In this case, the HTTP client must terminate the HTTP Stream and end the HTTP Request lifetime as a result of this message. The client application may add a delay before re-requesting the HTTP Stream.
Healthy chunked-encoded streams emit variable length chunks that contain parsable JSON.
Typically this will stream forever, unless a network interruption or service disruption occurs. It is up to the client to properly handle stream lifetime and connection closing.
In order to process these chunks, API consumers should first read the response buffer, then de-chunk the plain-text strings, and finally identify new JSON objects by applying tokenizing techniques to the resulting text stream using either a streaming JSON parser, Regex, a lexer/parser, or brute-force string indexing logic.
A simple but effective technique is after de-chunking to simply parse based
\n (newline character) delimiter written to the end of each
JSON object. However, a more robust solution is less likely to break later.
As a developer, be careful with how you parse HTTP Streams, because the API’s or intermediate proxies may chunk JSON objects many different ways.
Using HTTP streaming, several application messages can be sent within a single HTTP response. The separation of the response stream into application messages needs to be performed at the application level and not at the HTTP level. In particular, it is not possible to use the HTTP chunks as application message delimiters, since intermediate proxies might “re-chunk” the message stream (for example, by combining different chunks into a longer one). This issue does not affect the HTTP long polling technique, which provides a canonical framing technique: each application message can be sent in a different HTTP response.
Source: RFC6202, Section 3.2
Translation: Be prepared for JSON objects that span chunks. You may see chunks with varying numbers of JSON objects, including:
- "exactly 1" JSON object per chunk
- “at least 1” JSON object per chunk
- 1 JSON object split across 2 or more chunks
Example of 2 JSON objects in 1 chunk:
Example of 1 JSON objects split across 2 chunks:
This is allowed by the HTTP/1.1 specification, but can be confusing or lead to bugs in client applications if you try to depend parsing JSON along the HTTP chunk-boundaries because even if it works during testing, later if users connect from a different network, it may change the chunking behavior.
For example, if you are at a coffee shop with wifi which employs an HTTP Proxy, then it may buffer the stream and change the chunking boundary from 1 JSON object per chunk, to splitting each JSON object across 2 or 3.
In fact, the HTTP/1.1 spec clearly advises developers of proxies to always “re-chunk” HTTP Streams, so this is almost a guarantee to happen in the wild.
HTTP Streaming API consumers should be prepared to support all variations.