How do you receive and deserialize a `Flow` with ...
# ktor
s
How do you receive and deserialize a
Flow
with a ktor client? on the server side it seems to work seamlessly with
call.respond(flow(...))
but when I try
client.get("url").body<Flow<Object>>()
it fails with
kotlinx.serialization.json.internal.JsonDecodingException: Expected JsonObject, but had JsonArray as the serialized body of kotlinx.serialization.Polymorphic<Flow>
a
Can you describe your use case of the Flow in more detail? How does the server respond?
s
the usecase is to stream data for reduced latency, ie. I can display the first items as soon as they are received and do not have to wait for the entire list to finish. the server responds with a json array of objects, but sends each object individually and doesn't block until the entire list is fetched and processed from the db.
and on the server it works with just
call.respond(Flow<A>)
instead of manually using call.respondBytesWriter etc. so I'm wondering whether flows also work natively on the client
a
Unfortunately, the Ktor client doesn't support automatic conversion to Flow. However, you can use the code from this example to emit values within the flow as the response body is streamed.
s
I see. looking at the example, shouldn't it be
val channel: ByteReadChannel = httpResponse.bodyAsChannel()
instead of just
.body()
though?
the example doesn't work at all for me, it completely blocks until the entire incoming stream from the server is read
a
Can you share the code snippet you have so far?
s
@Aleksei Tirman [JB] sry, that was on me. I added an artificial delay to the source flow but the default buffer was so huge that it seemingly blocked until the entire response is received or until the request timed out
however, I haven't figured out how to deserialize the partial content back to usable kotlin classes yet