Sergey Aldoukhov
04/28/2023, 4:47 PMLuiz Aguiar
04/29/2023, 3:19 PMSergey Aldoukhov
04/30/2023, 9:45 PMfun Application.configureRouting() {
val rootGets = Channel<Int>()
val rootFlow = rootGets.receiveAsFlow()
var counter = 0
launch {
rootFlow.collect {
println(it)
}
}
routing {
get("/") {
rootGets.send(counter++)
call.respondText("Hello World!")
}
}
}
Luiz Aguiar
05/01/2023, 6:47 PMfun Application.configureRouting() {
routing {
get("/messages") {
call.response.headers.append(HttpHeaders.ContentType, "text/event-stream")
call.respond(produceMessages())
}
}
}
@Serializable
data class Message(
val number: Int
)
fun produceMessages() = flow<Message> {
(1..PRODUCTION_SIZE).forEach {
emit(Message(it))
}
}
The same behaviour that you have with Spring Webflux or Quarkus, the simplest SSE possible.
cc @Aleksei Tirman [JB] maybe have any hints to share 🙏Sergey Aldoukhov
05/01/2023, 6:51 PMLuiz Aguiar
05/01/2023, 7:04 PMcall.respond
is not enough.
TRACE i.k.s.p.c.ContentNegotiation - No suitable content converter found for response type class kotlinx.coroutines.flow.Flow and body kotlinx.coroutines.flow.SafeFlow
Sergey Aldoukhov
05/01/2023, 8:48 PMrouting {
var socketSession: DefaultWebSocketServerSession? = null
get("/") {
rootGets.send(counter++)
call.respondText("Catch it on socket")
listOf(1,2,3).asFlow().collect {
socketSession?.outgoing?.send(Frame.Text(it.toString()))
}
}
webSocket("/stream") {
socketSession = this
}
}