https://kotlinlang.org logo
#http4k
Title
# http4k
m

Marc

03/29/2022, 1:56 PM
hi everyone! i need opinion. i am enjoying http4k very much, specially the simplicity of the API. imagine i want to build a ws which has many events coming in and out (different sources in the background etc), my mind goes with reactive as it would be a natural way of thinking about flows of events. what would you suggest to use? callback hell from the socket handler, kotlin flow and trigger a coroutine from the handler and deal with flows from there back, rxjava/reactor or something else.
f

fredrik.nordin

03/30/2022, 10:17 AM
I have successfully used WS in http4k with coroutines and Kotlin Flow. Works like a charm!
m

Marc

03/30/2022, 11:06 AM
thanks!
f

fredrik.nordin

03/31/2022, 9:37 AM
Something like will work for getting the WS messages as a Flow:
Copy code
val messages = callbackFlow<WsMessage> {
  websocket.onClose { channel.close() }
  websocket.onError { error ->
    websocket.close(WsStatus(some code, some description))
    cancel(CancellationException("Websocket error", error))
  }
  websocket.onMessage { message -> trySendBlocking(message) }
  awaitClose {}
}
7 Views