Shubham Singh
03/08/2023, 5:35 PMwebSocket
block in Ktor Server and sendSerialized
the values collected from that flow? In my case, I am observing the changes inside my PostgreSQL DB and returning the changes as a flow from the repository. Now I want to send this flow of values through a websocket connection so that whenever the value inside the flow changes, my websocket also sends a new frame, but right now when I try to do the following:
webSocket(getObservable) {
safeCall {
val id = call.parameters["id"]
repository.getObservable(id = UUID(id)).collect { value ->
sendSerialized(value)
}
}
}
the websocket connection closes as soon as I connect to it.Andromadus Naruto
03/11/2023, 11:14 AMShubham Singh
03/11/2023, 12:12 PMAleksei Tirman [JB]
03/13/2023, 12:05 PMsafeCall
returns immediately. Here is an example where a mutable state flow is collected inside a websocket handler:
val flow = MutableStateFlow(0)
GlobalScope.launch {
while (true) {
delay(2000)
flow.update {
it + 1
}
}
}
embeddedServer(Netty, port = 8082) {
install(WebSockets)
routing {
webSocket("/ws") {
flow.collect {
send(Frame.Text(it.toString()))
}
}
}
}.start(wait = true)
Jonah Stiennon
03/13/2023, 7:47 PMShubham Singh
03/14/2023, 6:27 AMsafeCall
wasn't the issue, it is just a try-catch block internally. The thing was that I was getting only a Flow
from the repository (that is cold by nature), when I converted it into a SharedFlow using the shareIn
convenience function, it doesn't close immediately anymore, but now I'm facing another issue i.e. maybe the Apollo graphql client is not able to parse a UUID string as Json and hence throws the error in the image attached below. I'll maybe try to post this in the Apollo GraphQL slack channel.