#Web sockets and the Lichess API (on Android)

11 messages · Page 1 of 1 (latest)

thorny mica
#

As a learning project, I tried to develop an Android app (in Java) that can play games on Lichess.org using the Lichess API. Using OkHttp3, I managed to extract data from from the API. However, when actually playing games, you have to get Lichess to send you updates when a move has been made, and I got the impression that web sockets were the right technology for this. However, I keep getting the response

Response{protocol=http/1.1, code=200, message=OK, url=https://lichess.org/api/board/game/stream/<ID of my game>}

and the exception

java.net.ProtocolException: Expected HTTP 101 response but was '200 OK'

I was using an access token that had been given specific mandate to use the Board API.

My code can be seen in this topic on Stackoverflow where I also posted this previously, but didn’t get a reply yet:

https://stackoverflow.com/questions/77819221/web-sockets-and-the-lichess-api-on-android

grand portal
#

"I got the impression that web sockets were the right technology for this.",
😮
I haven't seen websockets mentioned in the Lichess API documentation... 🤔

thorny mica
#

Correct, but I have seen Lichess and web sockets mentioned many other places, incuding this Discord channel. But if there is a better way, feel free to suggest it.

grand portal
#

I've seen websockes mentioned too, and getting the Lichess account banned, in the same sentence 😱 (I think/hope it was a joke though... 😅 )

The documentation refers to the scheme "https" (not wss)
So I suggest "https".

And if/when you feel something is missing in the documentation, you could ask about it and/or suggest the addition of the feature, if you have a nice use case! 👍

thorny mica
#

But if I just make a single request to the stream/game/<gameID> API, I don’t get regular updates when a move has been made. I tried that. What I did was using the commented code in that StackOverflow post, just with the API replaced by stream/game/<gameID>.

#

In other words, I tried the following:

#

Request request = new Request.Builder()
.url("https://lichess.org/api/board/game/stream/" + gameId)
.header("Authorization", "Bearer " + access_token)
.header("Accept", "application/x-ndjson")
.build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            call.cancel();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            final String myResponse = response.body().string();
            System.out.println(myResponse);
        }
    });
#

But the method onResponse was never run (I tested this with the debugger)

grand portal
#

The endpoint streams the response with "neverending" lines of json - application/x-ndjson.
Does OkHttp3 have some API for reading that kind of response body? (I bet it does, OkHttp is awesome).

I use the standard HttpClient, and a BodyHandlers.ofLines() to get the response body,
(Here's links to Java 11, because I think at some point that's what the Android "Java" version was at...)
https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpResponse.BodyHandlers.html#ofLines()
https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html

thorny mica
#

I tried googling “OkHttp3 x-ndjson” and “OkHttp x-ndjson”, not much help there. Everyone everywhere on the web told me to use OkHttp on Android rather than the HttpClient, but maybe that’s not necessary

grand portal
#

Oh, when I wrote "I use the standard HttpClient" - I did not mean that I've used it on Android,
so sadly I don't know if the Android HttpClient has the same support which Java HttpClient has.
Hmm, disappointing that no hits on google - the search terms sounded good to me!
Tricky to know which avenue to investigate...
OkHttp3 or HttpClient...
⚖️