Nikolay Lebedev
10/27/2021, 9:37 PMembeddedServer
in my main function.
I guess I have to use a coroutine for regular database queries, but how do I then pass results to websocket connections?Andreas Scheja
10/27/2021, 10:22 PMStateFlow
and then just subscribe to that flow in your websocket handler?Nikolay Lebedev
10/27/2021, 11:19 PMAndreas Scheja
10/28/2021, 12:04 AMembeddedServer(Netty, port = 8080) {
val stateFlow = MutableStateFlow(0)
launch {
var i = 0
while (isActive) {
delay(Duration.ofSeconds(15))
stateFlow.update { ++i }
}
}
install(WebSockets)
routing {
webSocket("/some-route") {
stateFlow.collect { data ->
println("data was updated to $data")
send(Frame.Text(data.toString()))
}
}
}
}.start(wait = true)
right now its only an integer that keeps increasing every 15 seconds, but you should be able to get started from there