import kotlinx.coroutines.experimental.channels.ConflatedBroadcastChannel import kotlinx.coroutines.experimental.channels.consumeEach import kotlinx.coroutines.experimental.launch import kotlinx.coroutines.experimental.runBlocking sealed class ExampleEvent { object NormalEvent: ExampleEvent() data class NormalEventWithData(val i: Int): ExampleEvent() object ExceptionEvent: ExampleEvent() } fun main(args: Array) = runBlocking { val channel = ConflatedBroadcastChannel() // Listen to the channel launch { channel.consumeEach { event -> when (event) { is ExampleEvent.NormalEvent -> doStuff(0) is ExampleEvent.NormalEventWithData -> doStuff(event.i) is ExampleEvent.ExceptionEvent -> doException() } } } // Send some events to the channel launch { channel.send(ExampleEvent.NormalEvent) channel.send(ExampleEvent.NormalEventWithData(3)) channel.send(ExampleEvent.ExceptionEvent) channel.close() } } private fun doStuff(i: Int) {} private fun doException() {}