Would Kotlin Coroutines be a good candidate for a ...
# coroutines
a
Would Kotlin Coroutines be a good candidate for a Publish-Subscribe method of communication inside a monolith? Would that even work?
1
AFAIK coroutines are well suited to network calls, this wouldn't have network calls per se
n
What flavour of pub/sub are you used to?
a
We have a home-grown PubSub library that our team uses, it uses threading to be asynchronous. It's Java at the moment but I am wondering if we could benefit from Kotlin Coroutines
n
Do you want to rewrite your library using coroutines, or do you want to use your library as is in "user" code in Kotlin?
a
Well it uses completeable futures, so it's a little clunky.
So I guess rewrite
n
In isolation of the task, sure why not, if you're willing to pay the price of the rewrite to get better readability/quality of life to simplify maintenance and further development. (Or is your goal something else?) Given your specific task, possibly the Akka actor framework (Scala) is could be of use? I don't particularly endorse, just want to make you aware of options. There's an issue to revive the actor model in kotlinx.coroutines, but there's no timeframe on this. It has some implementation suggestions for a starting point. https://github.com/Kotlin/kotlinx.coroutines/issues/87
d
a
Alright I think I have my answer. Either an actor pattern (which I need to look into), an event bus pattern, but nothing out-of-the-box per-se. @Dumitru Preguza thanks for the link but it appears that hasn't been updated in a while
s
Would Dependency Injection with injected singleton StateFlow objects suit your case?
a
I'll check that out, thanks @Seri!
r
StateFlow is likely not suitable for an event bus as it conflates, use SharedFlow
👍🏻 1
💯 2
d
@ross_a How about Chanels vs SharedFlow ? Channels can have many producers from all over the app, whereas SharedFlow usually has just one source
r
Channels have each event consumed once, by only one consumer
There's nothing stopping you from calling
MutableSharedFlow.emit
or
MutableSharedFlow.tryEmit
(handle the return) from many sources or threads
👍🏻 1
s
You're right ross, SharedFlow is a closer match to the event bus pattern