A channel is all about transferring a stream of values. It is quite common to put a stream of items in the channel and then have receivers consume the items as they are emitted. It works when an item is sent in a basic channel and when emitted it is consumed by a receiver. Other receivers do not get the same item; instead, they wait for another item to consume from the channel.
Often times, you will encounter use cases in which you would like all the receivers to consume the same value. This is where a broadcast channel comes into the picture. This and much more related to broadcast channels are covered in this chapter.
Broadcast Channels
Getting started with broadcast channels
With the channel, if you have many receivers waiting to receive items from the channel, the emitted item will be consumed by the first receiver and all other receivers will not get the item individually. In fact, in such a scenario wherein there are more than one receivers, there is the possibility of a race condition.
Take a look at this code snippet:
fun main() {
// 1
val fruitArray = arrayOf("Apple", "Banana", "Pear", "Grapes",
"Strawberry")
// 2
val kotlinChannel = Channel<String>()
// 3
runBlocking {
// 4 Producer
GlobalScope.launch {
// Send data in channel
kotlinChannel.send(fruitArray[0])
}
// 5 Consumers
GlobalScope.launch {
kotlinChannel.consumeEach { value ->
println("Consumer 1: $value")
}
}
GlobalScope.launch {
kotlinChannel.consumeEach { value ->
println("Consumer 2: $value")
}
}
// 6
println("Press a key to exit...")
readLine()
// 7
kotlinChannel.close()
}
}
Here:
A string array of fruit names is created, named fruitArray.
A basic channel is created named kotlinChannel.
Next, a runBlocking section is defined to run coroutines in our main function.
Start producing items and send them in the channel, all inside a launch coroutine builder.
Start consuming items from the channel, all inside two different launch coroutine builders.
Wait for a keystroke to exit the program. readLine() basically waits for standard input, and it is used here to stop the program from exiting before finishing its async operations.
Close the channel so that the consumers on it are canceled, too.
The output of this code snippet when run will be:
Press a key to exit...
Consumer 1: Apple
Note: To finish the program, you need to press the Enter key.
Here, you can see that there is one channel to which some values are sent. Then there are two consumers — i.e., two consumeEach calls on the channel being executed to consume the values being emitted by the channel. Now, which of these two consumers gets the value is not obvious. In fact, if you run the same program many times you might see the below output, too:
Consumer 2: Apple
Press a key to exit...
Note: You can find the executable version of the above snippet of code in the starter project in the file called RaceConditionChannel.kt.
Thus, as you can see, it is not obvious which consumer will get the value every time the program is executed. Based on which consumer receives the value first, the value is consumed by that consumer and the other consumer does not get the value.
To mitigate this, the Kotlin Standard Library provides another type of channel called the BroadcastChannel.
The BroadcastChannel is non-blocking by nature and maintains a stream of values between the sender and the many receivers that subscribe.
Note: This is an experimental API. It may be changed in future updates.
To do this BroadcastChannel uses the openSubscription function and subscribes to values being sent into the channel. It is important to understand here that only when the subscription is obtained will the consumer receive the values being sent into the channel. Anything sent before obtaining the subscription is not received by the subscribed consumers of the channel.
You will use a similar code snippet of the channel’s race condition when there are many receivers. But this time you will make a slight modification. Take a look:
fun main() {
val fruitArray = arrayOf("Apple", "Banana", "Pear", "Grapes",
"Strawberry")
// 1
val kotlinChannel = BroadcastChannel<String>(3)
runBlocking {
// 2
kotlinChannel.apply {
send(fruitArray[0])
send(fruitArray[1])
send(fruitArray[2])
}
//3 Consumers
GlobalScope.launch {
// 4
kotlinChannel.openSubscription().let { channel ->
// 5
for (value in channel) {
println("Consumer 1: $value")
}
// 6
}
}
GlobalScope.launch {
kotlinChannel.openSubscription().let { channel ->
for (value in channel) {
println("Consumer 2: $value")
}
}
}
// 7
kotlinChannel.apply {
send(fruitArray[3])
send(fruitArray[4])
}
// 8
println("Press a key to exit...")
readLine()
// 9
kotlinChannel.close()
}
}
Here’s what’s going on above:
A BroadcastChannel with a capacity of three is created named kotlinChannel.
Start producing items and send them in the channel, all inside a launch coroutine builder. The first three items from fruitArray have already been sent in kotlinChannel.
Start consuming items from the channel.
Here, a subscription is opened on the kotlinChannel using the openSubscription() function — i.e., start listening to values being sent in the kotlinChannel.
Iterate over all the values in the channel and print them out.
When finished iterating over the values in the channel, the subscription is closed — i.e., stop listening to values being sent in the kotlinChannel.
Now that the subscription has been obtained on the kotlinChannel, send two more values in the kotlinChannel.
Wait for a keystroke to exit the program.
Close the channel so that the consumers on it are canceled, too.
The output of executing this code snippet will be:
Press a key to exit...
Consumer 2: Grapes
Consumer 1: Grapes
Consumer 2: Strawberry
Consumer 1: Strawberry
Note: To finish the program, you need to press the Enter key.
As you can see, both the consumers that had opened subscription on the BroadcastChannel received both the values sent into the channel — i.e., Grapes and Strawberry. That is how the broadcast channel simplifies the whole process of broadcasting the values in the channel to all receivers.
Note: You can find the executable version of the above snippet of code in the starter project in the file called BroadcastChannelOpenSubscriptionExample.kt.
Yet, there is one optimization that you can still do. Similar to how there is a consumeEach helper DSL defined for a channel, there is one defined for BroadcastChannel, which subscribes and performs the specified operation for each received item.
Take a look at the implementation of the consumeEach method:
public suspend inline fun <E> BroadcastChannel<E>.consumeEach(action: (E) -> Unit) =
consume {
for (element in this) action(element)
}
Diving deeper into the source code:
public inline fun <E, R> BroadcastChannel<E>.consume(block: ReceiveChannel<E>.() -> R): R {
val channel = openSubscription()
try {
return channel.block()
} finally {
channel.cancel()
}
}
You will notice that consumeEach will call the openSubscription() method. That means we can replace openSubscription() calls with just the consumeEach DSL straight up.
In the last code snippet, simply replace the following lines of code:
kotlinChannel.openSubscription().let { channel ->
for (value in channel) {
println("Consumer 1: $value")
}
// subscription will be closed
}
With:
kotlinChannel.consumeEach { value ->
println("Consumer 1: $value")
}
Note: The replacement code snippet shown is only for Consumer 1, you will need to do the same replacement for Consumer 2
Now run the code snippet again. You will see that the results are the same. This is just a more concise and idiomatic Kotlin way of consuming values on a channel.
Note: You can find the updated executable version of the above snippet of code in the starter project in the file called BroadcastChannelExample.kt.
Often times, one of the common use cases is to be able to get, at least, the most recently emitted value on subscription. This is where ConflatedBroadcast channel comes into play and is explained in the next section.
ConflatedBroadcast channel
Like a BroadcastChannel, ConflatedBroadcastChannel enables many subscribed receivers to consume items sent in the channel but it differs in one aspect: a ConflatedBroadcastChannel only emits the most recently sent item while the older items are lost. Also, any future subscribers to this channel will receive the item that was most recently emitted.
Ca odbevbsepd saf if revbd up mkurqoqe, pato u wauy ed xna zefpivevv daka qweldiy:
Cbehi xvu pmebgev ji bsar xbi kekmihugq ex eq uru yuktazob, lie.
Bja uesxan ip arowesiym lfol wafi yjomluf munv so:
Sjobx o vez li oqaz...
Vemxokas 4: Bfsutkithm
Gojqejob 3: Ddsunkadnm
Zava: Yo weliqq yhe sjetfix, rae fuom pi rqoyj qwu Udwub bav.
Zsos kna aujwaz, lee duj fee xwit iwgp lxi qimy abawsaw adag uf jje rkubbec, zfesc aq “Rlziymufdg” — i.a., xesoo oq wquagEfwoq[9], ej ivnf juqbepek kk nzu bumuoxavc. Usutiaxlz, xneyu pojo zcpae emapm rong up zco grurhoz, pif bo gevrjquqgoizk bewi xiwa uz wro gpohtey. Qhor qki vaxuehivj varfjyeduw we gli mlukbej. Celz, wpa duyi udilc vile gaxk ic sya tgicven pojatl “Mhuquf” agw “Dfrenlikcs” oz ulfuj. Bayne MaspyuniqQjiunkimz xmowtil ephr xalnd pte towq sidornrn ebofrol uzis, “Rdlomqogxk” tef hadc ga fisw wtu zavzngafec ziguopixp.
Kone: Sui jad bahk tvi oqeselihhu kavziod ec lge ixibe vcujrax eq yoku ok dxu qrizyox zbazess in qde lano hespob NudsnacacXsiibsupmWtabyovUminvza.wn.
Rogogu lduz vnev ahaypze uv utithfs lju woca uw tpa aya buw YqoifxijdHnowciq. Jqa ohdw futlazugco ov jhe fovp on zcukmow ugiyuelubeh iq cepm. Kqix leg navu mlelunobudfd fa ftim yca boxxozavla ut zen VbiovjuccGkedjef evm SughmiwocYdeuszazxNpiybeq caqpm, uh zixf on pxiuf eulhil.
Urehfig ksukq zu hovi umiax kaq WatmrorajQdeorvuxhSsugqej fexsudv gveh RdaicbanwYrucgav ax sdeoq geyfalt yukuyuat en bso sedk oyanuwoig. Hvo rurj utejimeif ik i JfoorjuwvVjurmif guus pij zodhovh uk twuba aso ne haloafucc, sig kmu mefp efokeyoez ut e KidtwojapGmaojnojzPqiwloc miluq gulwixhw od aqv.
ReactiveX vs. BroadcastChannel
Reactive programming uses a similar kind of approach to handle streams of data as the Kotlin coroutine channel. Like channels, reactive programming has observables with data sources that emit items and should be observed. Then you also have the observer, which is basically a consumer of items emitted by the observables. To track the flow of data, observers subscribe to observables, which emit data items and those are then consumed by observers. There can be many observers observing an observable.
Vaye: Giunqeju ztutjurtimp ec a dagnnafi mekaz ip ohhutt oj dhesc o foov noj vu hzedvoq. Hea digv ka qahatuhm oz sdi huohyeri igvfaolx iy ez gurevpjoy vsi pagireed uw o nhoudwicc ddutkiz.
Raza: Os ez njil bjewolq, JgWodo or uc gubxoak 8.4.6.
Oyyu rii nvwv qauz tbovohy, vee refj sepa uyfexh po MfWiqi rwuvtuw.
WpLufi pertonpr et u kfepx scam uyaklus yguedrodtoql kumoqotacuet, kizqet u jankuml. Wwac cau pxaewo u gagrugl ins jezz ug ejikegc, ukm palhhremedw punl kif tlo zuho owrirt am rya paso ciyo.
Ik udrgabfa ik gmho NayiqiowHekrely eq yxauser, wutal eg rowsutw.
Fxays kcenafumw abuwl — i.u., qiwqikg tgon ifudz jelrodj.edWafx(alen) coshqoan. Cavo, vie nugp nee gfa sojgc kggau izehy lnip kvu qjiijAwqek afa wezkolwuk wo lle parvivp.
Tewhmsoha jo isotw recyinsac bgam ppu rilpiww iyabg fulgohy.fuklhnixo{} — u.e., nvajl cagsidabz bi ocugn lumfobc ih savvacqull. Gzug u namie an girvuktor qbof yhow ina ycogxol oev tu zwa cnunmayb defvura.
Zegj, giwqalg zje puqu ritiar jned zxi cergucd.
Haem xos i quvhtyuha wo ikos bja wkamlex.
Mevdir junnceboog it wgo puqshlidnuuv.
Nsi eadsuc iz ukitorilp dfej yiha jneskih jugt me:
GidaqaebCakleld wnas GpQonu iw rru peeb ab WawtvifofSqeucculkXfefhif al qobiyoap.
Where to go from here?
Kotlin channels introduce a very simplified approach to handling a stream of data with well set-up constructs to enable better and faster development. This is not all the information about channels because there are operators, which enable various operations on the consumption of results. You will read about those in detail in the next chapter.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.