BroadcastChannel

interface BroadcastChannel<E> : SendChannel<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 API is obsolete since 1.5.0. It will be deprecated with warning in 1.6.0 and with error in 1.7.0. It is replaced with SharedFlow.

Functions

cancel
Link copied to clipboard
abstract fun cancel(cause: CancellationException? = null)

Cancels reception of remaining elements from this channel with an optional cause. 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. A cause can be used to specify an error message or to provide other details on a cancellation reason for debugging purposes.

openSubscription
Link copied to clipboard
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.

Inheritors

ConflatedBroadcastChannel
Link copied to clipboard

Extensions

asFlow
Link copied to clipboard
fun <T> BroadcastChannel<T>.asFlow(): Flow<T>

Represents the given broadcast channel as a hot flow. Every flow collector will trigger a new broadcast channel subscription.

consume
Link copied to clipboard
inline 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.

consumeEach
Link copied to clipboard
inline suspend fun <E> BroadcastChannel<E>.consumeEach(action: (E) -> Unit)

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

Sources

common source
Link copied to clipboard