How to achieve long polling in Android with rxKotlin?

56 Views Asked by At

When I request api, the Server will send a response after 10 seconds. And after the client got the response, I had to request back the API immediately.

Like this:

    fun getSampleApi() {
            sampleApiService.requestApi(
                Params()
            ) // Observable
            .toFlowable()
            .delay(3, TimeUnit.SECONDS)
            .distinctUntilChanged()
            .subscribeOnIo()
            .observeOnIo()
            .subscribeBy(onNext = { streamResult ->
                getSampleApi() // recurse
            }, onError = {})
    }

I intended to request API immediately (recursively, maybe this caused the problem) when I got 200 responses from the server. With this code, the problem is, there is extremely many overlapped API request.

How can I make this sequentially, without overlapping requests? with RxKotlin or RxJava.

0

There are 0 best solutions below