lichess.org
Donate

API not returning response in reasonable time.

I am attempting to write my own Chess GUI, and have setup a simple script to begin a game against the Lichess AI and play the move 1. e2e4. This much works and I am able to successfully start the game and play my move, receiving a 200 OK status code, however when I try to steam the game (via the board/game/stream endpoint) I do not receive a response until the game is over. No matter what settings I use, I always get a response AFTER the game has ended, after about 3-5 minutes (proportional to game length). After the game ends I receive consistent updates in about 100-200ms. I am not able to stream receive updates for my opponent's moves on my own game because of this.
Is this intentional?
I have already verified that I'm using the correct endpoints, and am receiving responses from all other endpoints in a timely manner.
Is there any other way to receive my opponent's moves besides streaming the game?
Hiya!

One guess which kind of fits with your description,
is that your HTTP client isn't handling new-line delimited json data properly...
It might be expecting that the response should be of fixed size (have a content-length),
but that is not the case for the streaming endpoints of Lichess.

Many other of the endpoints of the Lichess API have responses which are simple single lines of JSON,
where the response has a header which shows the content-length and then that many bytes are sent from the server to your client and the request is finished.
But the streaming endpoints don't end, they don't have a fixed content-length.
They continuously writes lines of json for each happening event (such as moves being played) without closing the response body.
Only when the game finishes, the server closes the stream.
So your HTTP client (and script/application) needs to be able to handle this type of content type ("application/x-ndjson") which comes in line by line.

Here's a section in the documentation which mentions the streaming of events and provides utility functions how to read these streams using JavaScript,
lichess.org/api#section/Introduction/Streaming-with-ND-JSON

Hope this guess is accurate and that this piece of information can be the beginning of your journey on the path towards success!
Chess on!