I would like to use http4k to stream a long response. I plan to use Content-type: multipart/x-mixed-replace
so I push data to the client quite endlessly. In http4k, we have typealias HttpHandler = (Request) -> Response
. But my handler cannot return a response because it is not a limited document that I want to return but an endless stream. Is this means that I should use something else for what I want?
Asked
Active
Viewed 749 times
2

Pierre Thibault
- 1,895
- 2
- 19
- 22
2 Answers
3
If you're pulling from another HTTP source, you can use the streaming body mode on one of the various HTTP client modules (Apache/OkHttp/Jetty will work).
Alternatively if you're generating the content yourself or streaming from a database, you'll have to start a Thread and handle it that way. There's an example of how to do this in the source code in a test case that is used to prove the various clients can do streaming.

David D
- 239
- 2
- 4
-
Great. It didn't look in `test`. I thought it was only about testing. – Pierre Thibault Nov 30 '19 at 15:14
-
1Tests are about proving the features that the library supports are working and thus always serve as some kind of documentation about the APIs. True, there could probably be more docs about this. Will try to add something to the examples in the cookbook. – David D Dec 01 '19 at 16:07
-
This is not what I want. I want my server to be able to stream. – Pierre Thibault Dec 06 '19 at 14:17
-
Code to stream from the server is here - https://github.com/http4k/http4k/blob/master/http4k-core/src/test/kotlin/org/http4k/streaming/StreamingContract.kt#L106 – robd Mar 01 '23 at 16:42
1
Could be that websocket is what you need?
https://www.http4k.org/blog/typesafe_websockets/
So you can have an endless stream of event (e.g you need to push a feed).

sciack
- 83
- 6