Hi, does http4k client support receiving SSE? If s...
# http4k
s
Hi, does http4k client support receiving SSE? If so, is there any information on how to implement that? I can't seem to find any info on that on the project site (only the server side or so it looks for me)...
d
nope. 🙂
r
Haven’t try it but found this Java example https://github.com/nicferrier/java-simple-sse-client
This OkHttp extension seems simpler to use as inspiration https://github.com/heremaps/oksse
I did a very fast rip off of the OkHttp one (just quick fixes of IntelliJ Kotlin from Java transcoding) https://github.com/razvn/http4kbase/tree/test_sse calling from a browser http://localhost:9000/{name} will launch a SseClient that calls the SseServer with the same url http://localhost:9000/{name} that sends some data and events. The thing I see so far that does not work as I expected, when the client closes didn’t see the server onclose message sent to the client.
Why didn’t I think searching
Kotlin SSE client
directly instead of Java… I would have found directly this implementation… https://github.com/biowink/oksse
Fan fact as it’s http, you can also just do a long stream with with the current client and just parse the received strings
Copy code
object StreamSSE {
    operator fun invoke(req: Request) {
        val request = req
            .replaceHeaders(
                listOf(
                    "Accept-Encoding" to "",
                    "Accept" to "text/event-stream",
                    "Cache-Control" to "no-cache",
                )
            )
        val noTimeoutClient = PreCannedOkHttpClients
            .defaultOkHttpClient()
            .newBuilder()
            .readTimeout(Duration.ZERO)
            .build()
        OkHttp(client = noTimeoutClient, bodyMode = BodyMode.Stream).debug()(request).body.stream.bufferedReader().forEachLine {
            println(it)
            if (it.startsWith("End")) throw Exception("End")
        }
    }
}
đź‘Ť 1