is there something like a channel but which can ha...
# announcements
a
is there something like a channel but which can have two receivers?
g
Yes, BroadcastChannel
a
ok also kinda confused on when to use channels vs flows... think I should use a channel for events which go on indefinitely?
or at least turn a flow into a broadcast channel?
with
Flow#broadcastIn(...)
g
Flow is cold stream, it doesnt have multiple receirs by definition
But you can open Flow to BroadcadtChannel
broadcastChannel.asFlow()
Turning flow to broadcast channel possible too, yes, what is your use case
Flow is cold stream, Channel is hot
a
well I have something like this which polls email subjects... iyo is it better for this to be a Flow or a Channel?
g
Also, I would recommend to ask in #coroutines
a
aight
g
In this case I would use Flow
It's lazy
a
isn't this basically "hot" data though? Or technically cold because produced with a function
g
And for cases when I want to share it between multiple receirs (request email once and broadcast to all receivers), ai would convert it to BroadcadtChannel
a
usually I see hot data as events and whatnot which this kinda is I guess
aight
g
As I said, depends on case, but I don't see why this case should be hot in the first place
But for some use cases it's fine to convert it to hot (broadcast, or even conflated broadcast)
a
I mean cold data you need to go through all data right? and this might be bad since you are getting data indefinitely
maybe I am not understanding hot vs cold properly
g
Not necessary all data, you can close flow when you want. like you can dispose RxaJava observable, the same way you can cancel coroutine where you consuming Flow, and good thing about flow in this case that it will start only when someone consumes it (your channel above will be started eagerly, and eagerly compute first value and continue invocation until this channel will not be closed, for example in case of iterator usage on channel it will not be closed) and will be finished when consumer will be cancelled
👍 1
And you can use cold streams for infinite streams of data with no problems, but this stream will be started when someone consumes it and finished when consumer will be stopped, and without some hot adapter cannot be consumed twice or by multiple receivers simultaneously