How do I send/receive HTTP/2 streams using ktor?
# ktor
t
How do I send/receive HTTP/2 streams using ktor?
đź‘€ 1
s
Your main point is streaming or performance? AFAIK HTTP/2 is not related directly to ktor and is something that is supported/configured on the engine level. If you’re looking at streaming, you should use StreamingOutput or SSE or WebSockets. HTTP/2 might improve performance of these methods, but it does not provide you with streaming by itself.
t
@Sergey Aldoukhov It's more about streaming. I would like to transmit (potentially unbounded) streams of data bidirectionally between a ktor server and a ktor client
s
In Ktor, you can both do call.receiveStream() and call.respondOutputStream. Of course, these should be separate handlers, and the final implementation depends on the exact details of what you want to achieve. This is how you’d stream from server to client:
Copy code
get("/") {
                call.respondOutputStream {
                    // You can write response to this output stream
                    // Note: It should be done in a separate thread
                    it.bufferedWriter().use { writer ->
                        writer.write("Hello world!")
                    }
                }
            }
Another approach is to use Websockets, which is better in a sense you can do both sending and receiving using the same socket connection, but it will be not as performant as using respondOutputStream
228 Views