Difference Between Flows and Channels in Kotlin

Abhishek Srivastava
4 min readMay 7, 2023

Flows are cold stream while channels are hot stream.

This single line definition is not enough to know about them. But before start discussing about the differences between Flow and Channel, First know about the Hot and Cold Stream.

Cold Stream Flow vs. Hot Stream Flow

In Cold Stream Flow, in the case of multiple collectors, the complete flow will begin from the beginning for each one of the collectors, do the task and emit the values to their corresponding collectors. It’s like 1-to-1 mapping. 1 Flow for 1 Collector. It means a cold flow can’t have multiple collectors as it will create a new flow for each of the collectors.

A cold stream does not start producing values until one starts to collect them.

In Hot Stream Flow, in the case of multiple collectors, the flow will keep on emitting the values, collectors get the values from where they have started collecting. It’s like 1-to-N mapping. 1 Flow for N Collectors. It means a hot flow can have multiple collectors.

A hot stream on the other hand starts producing values immediately either any one is collecting it or not.

Now come back to the our point and now want to know about the difference between Flow and Channel.

Now discuss one by one for Flow and Channel.

Flow

Flow do not inherently involve any concurrency. They are non-blocking, yet sequential. The goal of flows is to become for asynchronous data streams what suspending functions are for asynchronous operations — convenient, safe, easy to learn and easy to use.

Flow are executed sequentially inside the same block of code (and, therefore, the same Coroutine Scope).

An important characteristic of Flow is that it only starts emitting values when someone asks for them. Before someone subscribes (or performs a collection), the Flow doesn’t run its code.

That’s why Flow is called as Cold Stream.

Flow mostly used for the callback approach.

Channel

Channels are a great fit to model data sources that are intrinsically hot, data sources that exist without application’s requests for them: incoming network connections, event streams, etc. ChannelFlows can have multiple coroutines running inside and could produce data from outside the stream. Channels, just like futures, are synchronization primitives. You shall use a channel when you need to send data from one coroutine to another coroutine in the same or in a different process.

Channel Flow is used for the concurrent flow emission.

Difference Between Flows and Channels

  1. Flows are a good fit for sequential data processing while Channel are good fit for concurrent data flow processing.
  2. Flow will not send any data until any receiver is not available to receive it while it is not the case for channel. Channel can emit data no matter any receiver is available or not.
  3. Channels aren’t scoped to a specific lifecycle, unlike flows. If we want to close/cancel a Channel, we need to explicitly use the methods provided by the Channel API. So, let’s say we need to have a data stream that survives across the screen lifecycle, then we have to use channels while On the other hand, if we need to scope the emissions to a certain component’s lifecycle, flows are handy. For instance, if we’re using the MVVM architecture and viewModelScope, all the flows started with that scope will be canceled when ViewModel gets cleared.
  4. Channel stream is like a buffer, which has the buffer capacity. The default buffer capacity is zero. By default, when the buffer is overflow, the data sending is suspended until there is a receiver.
  5. Whenever Flow<T>collect() is called, a new cold stream is created and data collected into the new stream while for channel receiver does not create a new stream, It still uses the existing stream.

For know more about it, Click here.

Thanks for reading…

--

--

Abhishek Srivastava

Senior Software Engineer | Android | Java | Kotlin | Xamarin Native Android | Flutter | Go