Does someone use SSE (Sever-sent Events) together ...
# ktor
s
Does someone use SSE (Sever-sent Events) together with Ktorfit? I have an interface like this:
Copy code
@GET("plans/{id}/status")
fun getPlanStatusStreamById(@Path("id") id: String): Flow<BackupStatus>
The server sends the JSON as server-sent events. I have some AI generated Flow Converter, but that doesn't work at all.
Copy code
class SseConverterFactory(
    private val json: Json
) : Converter.Factory {

    override fun responseConverter(
        typeData: TypeData,
        ktorfit: Ktorfit
    ): Converter.ResponseConverter<HttpResponse, *>? {

        if (typeData.typeInfo.type != Flow::class)
            return null

        println("Found Flow type: ${typeData.typeInfo}")

        return object : Converter.ResponseConverter<HttpResponse, Flow<Any>> {

            override fun convert(
                getResponse: suspend () -> HttpResponse
            ): Flow<Any> {

                return flow {

                    val response = getResponse()
                    println("SSE RESPONSE: $response")

                    val sseSession = response.body<ClientSSESession>()
                    println("SSE SESSION: $sseSession")

                    sseSession.incoming
                        .mapNotNull { event ->

                            event.data?.let { data ->
                                try {
                                    json.decodeFromString<Any>(data)
                                } catch (e: Exception) {
                                    println("Error decoding event: $e")
                                    null
                                }
                            }
                        }
                        .collect { backupStatus ->
                            emit(backupStatus)
                        }
                }
            }
        }
    }
}
So what's the proper solution?
j
How would it look in basic ktor, without ktorfit?
s
Bild von iOS.jpg
I like their API for that 🙂
j
Hmm...
Ktorfit already has a converter for Flow
Are you sure you need it?
I wonder if your custom converter is conflicting with the built-in one
Hm, looking more deeply, I think this is a significantly more complex thing. SSE is more intrusive to the HttpClient than a RequestConverter can do
👀 1
At minimum you'll need to interact with the
@Streaming
annotation: https://foso.github.io/Ktorfit/requests/#streaming
Ah, I see you already opened an issue. This is about all I can do, so good luck!
thank you color 1
s
Yes, the streaming API is not enough in this case.
I wonder if your custom converter is conflicting with the built-in one
I tried the provided FlowConverter, but that thing is not intended for this protocol.