Search code examples
spring-webfluxkotlin-coroutinesproject-reactorrsocket

Spring WebFlux using RSocket: Kotlin coroutines Flow vs Reactor Flux message format


Is there any difference in IO traffic when we use Spring WebFlux's RSocket (over WebSockets) support to emit values using Kotlin Coroutines Flow and Reactor Flux?

@MessageMapping("stream")
suspend fun send(): kotlinx.coroutines.flow.Flow<SomeClass> = ...

VS

@MessageMapping("stream")
fun send(): reactor.core.publisher.Flux<SomeClass> = ...

Also, should the client side code (JS with rsocket-websocket-client) be different depending whether the server uses Kotlin Coroutines Flow or Reactor Flux?


Solution

  • No, they should be the same. Spring should take care of the differences between them. That said, if there are any bugs you observe you should raise them.

    It should not be possible for client code to observe whether the server is defined using Flux or Flow. Further to this, hopefully a client isn't aware of the implementation language of the server either.

    I don't think your first example needs to be suspending either, as the Flow will normally be cold anyway. https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/

    The Flow interface does not carry information whether a flow is a cold stream that can be collected repeatedly and triggers execution of the same code every time it is collected, or if it is a hot stream that emits different values from the same running source on each collection. Usually flows represent cold streams