Open ashNext opened 2 years ago
I have a gRPC Kotlin sample that uses BiDi for a chat client & server: https://github.com/jamesward/cloud-native-grpc-kotlin
Maybe that can help you figure out what is going on?
@jamesward hi, thx for reply, but your example is very simple, how can I get chatId for my shared flows? i need many chats
Architecturally there are a lot of variations. In a real-world I'd use Kafka and use that as the hub. For a single-node configuration you can do something like you and I have but you should definitely use MutableSharedFlow
as the hub, like:
https://github.com/jamesward/cloud-native-grpc-kotlin/blob/main/chat-server/src/main/kotlin/cngk/ChatServer.kt#L17
Note how the sharedFlow.emitAll(filteredFlow)
has to be run in a separate coroutine.
@jamesward how can I get chatId from requests flow in first request? I see only a way like this:
sharedFlow.combine(requests) { a, b ->
b
}.collect {
println(it.t)
}
}
return sharedFlow
but this check id every message on collect. Is there any way to get chatId in the first message and than save sharedFlow per chatId only once?
I'm not totally understanding your architecture and what you mean by chatId
. Can you provide more details on that?
There is method with bidi streaming for message exchange for clients. Client send message to this method with chatId
, and I need send this message only for other clients with this chatId
. In your example you send message to ALL
I see. Yeah, you will have to put that state somewhere. Map<String, MutableSharedFlow>
might work but you'll have to be careful with concurrency. Ultimately this approach is only good for a toy. For prod you'll need to externalize that state or use Kafka (etc).
No, I cannot. Because return of the flow called before collect. Or can you give some example with code please?
I want to make a simple chat where one client sends a message and other clients receive it. I have done this on Bidirectional streaming RPC and StreamObserver via the ... ServiceImplBase implementation.
ChatServer.kt
But to do the same through Flow and implementation ... ServiceCoroutineImplBase does not work. I don't understand how to "remember" streams.