BroadcastChannel

@ExperimentalCoroutinesApi interface BroadcastChannel<E> : 
    SendChannel<E>
(source)

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

isClosedForSend

abstract val isClosedForSend: Boolean

Returns true if this channel was closed by an invocation of close. This means that calling send or offer will result in an exception.

onSend

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

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

Functions

cancel

abstract fun cancel(
    cause: CancellationException? = null
): Unit

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

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

close

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

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

invokeOnClose

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

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

offer

abstract fun offer(element: E): Boolean

Immediately adds the specified element to this channel, if this doesn’t violate its capacity restrictions, and returns true. Otherwise, just returns false. This is a synchronous variant of send which backs off in situations when send suspends.

send

abstract suspend fun send(element: E): Unit

Sends the specified element to this channel, suspending the caller while the buffer of this channel is full or if it does not exist, or throws an exception if the channel is closed for send (see close for details).

Extension Functions

asFlow

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

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

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

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

Inheritors

ConflatedBroadcastChannel

class ConflatedBroadcastChannel<E> : BroadcastChannel<E>

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