https://kotlinlang.org logo
#ktor
Title
# ktor
j

Janez Štupar

09/11/2018, 2:07 PM
Hi there! I have already tried asking in RX channel, but nobody responded. I am trying to call an Observable pipeline from a Ktor request handler. The problem is that response writer is already closed before Observable produces a result, how do I go about this?
r

robin

09/11/2018, 2:10 PM
Do have to use an Observable, or could you switch to coroutines-only? If you need to use an observable, you'll have to bridge that into coroutines, by using the
suspendCoroutine
function from the ktor request handler, and resume the coroutine inside that once the observable completes. Something like this (from memory, so might not be completely correct):
Copy code
val observable: Observable
val observableResult = suspendCoroutine { continuation ->
    observable.subscribe { it -> continuation.resume(it) }
}
j

Janez Štupar

09/11/2018, 2:14 PM
I have to use Observable because the API I am attempting to use is based on RxJava. Unless I am misunderstanding somehow and there is a way of transforming RX operators into coroutines?
r

robin

09/11/2018, 2:15 PM
No there are no standard operators, you'll have to do what I explained then, use
suspendCoroutine
to bridge the two worlds 🙂
j

Janez Štupar

09/11/2018, 3:38 PM
Thanks, this seems to work!