<@U017TSBJ2C9> Asked this question in YouTrack tic...
# ktor
z
@Aleksei Tirman [JB] Asked this question in YouTrack ticket comment and not get the answer, reposting here in the channel: Regarding to the Ktor 3.0 SSE support: 1. What does the SSE plugin offer specifically? What's wrong with using:
Copy code
val res = serviceClient.post(url) {
            headers {
                append(HttpHeaders.Accept, "text/event-stream")
            }
            contentType(ContentType.Application.Json)
            setBody(requestBody)
        }

val channel: ByteReadChannel = res.body()
while (!channel.isClosedForRead) {
     channel.readUTF8Line()?.let { line ->
          // process SSE chunk
     }
}
2. Does the above code behave the same in Ktor 2.3.x and Ktor 3.0? i saw the [original request of implementing SSE](https://github.com/ktorio/ktor/issues/1517), and some one pointed out, it is impossible to implement true SSE with Ktor 2.3.x, and it's fixed only in Ktor 3.0. Does that mean, with Ktor 3.x, the above code function as expected?
a
1. The SSE plugin offers a convenient API to work with SSE. 2. It should behave the same. Please file an issue if you observe any abnormal behavior.
z
@Aleksei Tirman [JB]
So, as SSE is a very simple protocol, I tried to implement this, but it turns out it's impossible to implement, because most (if not all) of the Ktor client engines buffer the responses. This is fine for streaming file downloads, etc., but to implement SSE it has to be possible to stream the response byte-for-byte, one-by-one.
Ktor itself has an API to read a single byte (
ByteReadChannel.readByte
), so it's possible to write something that LOOKS like it will work, but it won't work, because the client engines will suspend until they fill an arbitrary buffer (or the connection is closed).
And this is only fixed in 3.0, so supposedly, the code above in Ktor 2.3.x doesn't really work, it buffers and output larger chunks. Isn't it?
a
The code above is incorrect because it doesn't stream the response. Here is a correct one that works for both versions equally:
Copy code
val request = serviceClient.preparePost(url) {
    headers {
        append(HttpHeaders.Accept, "text/event-stream")
    }
    // ...
}

request.execute { res ->
    val channel: ByteReadChannel = res.body()
    while (!channel.isClosedForRead) {
        channel.readUTF8Line()?.let { line ->
            println(line)
        }
    }
}
👍 1
n
I'm not sure if this is a known issue, but I see duplicate Content-Type headers when trying SSE with 3.0.0-beta-1.
Copy code
HTTP/1.1 200 OK
Vary: Origin
Content-Type: text/event-stream <-------
Cache-Control: no-store
Connection: keep-alive
Content-Type: text/event-stream <-------
transfer-encoding: chunked
This causes the following exception in the JS client:
Copy code
SSEException: Expected Content-Type text/event-stream but was: text/event-stream, text/event-stream
This comes from installing the
SSE
plugin on the client/server and sending an sse response as follows:
Copy code
sse {
    send("Hello")
}
a
There were some Content-Type issues related to the SSE functionality (https://github.com/ktorio/ktor/pull/3866). Are you able to reproduce the problem with the version
3.0.0-beta-1-eap-879
?
n
Which repository is that build in? I'm not finding it in https://maven.pkg.jetbrains.space/public/p/ktor/eap.
n
This does still reproduce, even in
3.0.0-beta-2-eap-884
.
I'm also seeing another issue where a client that has SSE installed cannot be used for normal get/post/etc. requests. I tried this and am getting
SSEException: Expected Content-Type text/event-stream but was: application/json
when executing a
get
with the client to an endpoint that returns json. I'd be surprised if this were the expected behavior.
a
Can you please file an issue about the duplicated Content-Type header?
I'm also seeing another issue where a client that has SSE installed cannot be used for normal get/post/etc.
This problem is known (https://youtrack.jetbrains.com/issue/KTOR-6614).
👍 1
n
Also, are there any plans to add support for what's called out in (https://youtrack.jetbrains.com/issue/KTOR-6056)? Or support automatic (SSE) reconnection from the client?
File: KTOR-6735 Duplicate Content-Type Headers.
a
No specifics plans for that. You can find an issue about the automatic reconnection here.