@ExperimentalCoroutinesApi interface BroadcastChannel<E> : 

Broadcast channel is a non-blocking primitive for communication between the sender and multiple receivers that subscribe for the elements using openSubscription function and unsubscribe using ReceiveChannel.cancel function.

See BroadcastChannel() factory function for the description of available broadcast channel implementations.

Note: This is an experimental api. It may be changed in the future updates.

Inherited Properties


abstract val isClosedForSend: Boolean

Returns true if this channel was closed by invocation of close and thus the send and offer attempts throws exception.


abstract val isFull: Boolean

Returns true if the channel is full (out of capacity) and the send attempt will suspend. This function returns false for isClosedForSend channel.


abstract val onSend: SelectClause2<E, SendChannel<E>>

Clause for select expression of send suspending function that selects when the element that is specified as parameter is sent to the channel. When the clause is selected the reference to this channel is passed into the corresponding block.



abstract fun cancel(cause: Throwable? = null): Boolean

Cancels reception of remaining elements from this channel. This function closes the channel with the specified cause (unless it was already closed), removes all buffered sent elements from it, and cancels all open subscriptions. This function returns true if the channel was not closed previously, or false otherwise.


abstract fun openSubscription(): ReceiveChannel<E>

Subscribes to this BroadcastChannel and returns a channel to receive elements from it. The resulting channel shall be cancelled to unsubscribe from this broadcast channel.

Inherited Functions


abstract fun close(cause: Throwable? = null): Boolean

Closes this channel. This is an idempotent operation – repeated invocations of this function have no effect and return false. Conceptually, its sends a special “close token” over this channel.


abstract fun invokeOnClose(
    handler: (cause: Throwable?) -> Unit
): Unit

Registers handler which is synchronously invoked once the channel is closed or receiving side of this channel is cancelled. Only one handler can be attached to the channel during channel’s lifetime. Handler is invoked when isClosedForSend starts to return true. If channel is already closed, handler is invoked immediately.


abstract fun offer(element: E): Boolean

Adds element into this queue if it is possible to do so immediately without violating capacity restrictions and returns true. Otherwise, it returns false immediately or throws exception if the channel isClosedForSend (see close for details).


abstract suspend fun send(element: E): Unit

Adds element into to this channel, suspending the caller while this channel isFull, or throws exception if the channel isClosedForSend (see close for details).

Extension Functions


fun <E, R> BroadcastChannel<E>.consume(
    block: ReceiveChannel<E>.() -> R
): R

Opens subscription to this BroadcastChannel and makes sure that the given block consumes all elements from it by always invoking cancel after the execution of the block.


suspend fun <E> BroadcastChannel<E>.consumeEach(
    action: (E) -> Unit
): Unit

Subscribes to this BroadcastChannel and performs the specified action for each received element.


fun <E> SendChannel<E>.sendBlocking(element: E): Unit

Adds element into to this channel, blocking the caller while this channel Channel.isFull, or throws exception if the channel Channel.isClosedForSend (see Channel.close for details).



class ConflatedBroadcastChannel<E> : BroadcastChannel<E>

Broadcasts the most recently sent element (aka value) to all openSubscription subscribers.