Hi there! I have trouble integrating synchronous c...
# coroutines
u
Hi there! I have trouble integrating synchronous code with coroutines, in order to get that broadcasting feature I was able to implement using `RxJava`:
Copy code
class Handler : MessageHandler {

    private val channel = Channel<Message>(1)

    override fun handle(message : Message) {
        // TODO send event.
    }

    fun send(event : Message) {
        
    }

/*
    fun observe() : Channel<Message>
    fun observe() : Flow<Message>
*/
}
When having `RxJava`’s
subjects
, I could call
onNext()
, let observers consume it by exposing some
Observable
. Is there a way to do the same with
Coroutines
? I need to expose some methods that allow to observe stream of events received in
handle(message : Message)
method. Thanks a lot in advance!
z
expose your channel into a flow (
consumeAsFlow()
or
receiveAsFlow()
)
send data to teh channel
if you need subject-like behavior
u
@zak.taccardi, thanks for helping. Could I implement that without marking my method with suspend modifier?
z
yes
u
@zak.taccardi, how do you send events to your channel?
z
with
channel.send(..)
if I am in a coroutine or
channel.offer(..)
if I am not
u
thanks!
z
note that if you use
channel.offer(..)
, the Channel’s capacity comes into play
💥 1
u
could we implement some kind of coroutines bus using channels?
z
not just with Channels, you’d need
Flow<T>
Channels are for communicating between coroutines, or into one from outside of one, and
Flow<T>
is for exposing an API of multiple events of
T
Channels are a communication primitive
p
check this for an event bus based on coroutines: https://github.com/Daniil-Pavenko/coroutines-event-pipe
However, although Event Bus is a great architecture on Server Side. Android popular implementations I consider are very poor.