SharedFlow: A Hot Flow for Broadcasting Events

A SharedFlow is a hot stream that broadcasts values to all subscribers, like a live TV channel. It's ideal for one-to-many events, like UI updates or notifications. The main footgun: it never completes, so collect will suspend forever.
Why it exists
Sometimes you need to send a single stream of events to multiple parts of your app at the same time. A regular, "cold" Flow is unsuitable because it creates a new, independent execution for each collector. SharedFlow solves this by providing a single source that broadcasts values to all current and future collectors.
The mental model
Think of a SharedFlow as a live TV broadcast. The station (the flow) is always on and emitting a signal, regardless of how many people are watching. Subscribers (collectors) can tune in at any time and start receiving the live signal. If the flow is configured with a replay cache, a new subscriber is like rewinding a DVR to see the last few moments before catching up to the live broadcast.
How it works
A SharedFlow is created using a MutableSharedFlow() constructor or by applying the shareIn operator to a cold Flow. The common pattern is to keep a private MutableSharedFlow for emitting values and expose a public, read-only SharedFlow for collection. You can configure its behavior on creation. The replay parameter sets how many recent items are cached and sent to new subscribers. The extraBufferCapacity adds a buffer to handle slow subscribers without blocking the emitter. If the buffer fills, the onBufferOverflow strategy determines the outcome: SUSPEND (the default), DROP_OLDEST, or DROP_LATEST.
When to use it
Use SharedFlow for one-to-many event distribution where multiple, independent parts of your app need to react to the same event stream. This is perfect for broadcasting UI events (like a logout signal), receiving real-time server updates, or implementing a simple in-memory event bus for your application.
When not to use it
If you only need to represent a single, observable piece of state, use StateFlow. It's a specialized SharedFlow designed for that exact purpose. If each consumer needs its own separate, fresh execution of the data stream from the beginning, use a standard cold Flow.
One canonical example
A common use case is an event bus. You can create a class with a private MutableSharedFlow to control emissions and a public SharedFlow for subscribers. For example: private val _events = MutableSharedFlow<Event>() and val events = _events.asSharedFlow(). A public function like produceEvent(event) would then call _events.emit(event) to broadcast the event to all listeners.
Interview question
Which scenario is the most appropriate use case for a Kotlin SharedFlow?
- a.Executing a complex calculation once and providing its result to a single consumer.
- b.Broadcasting real-time user authentication status updates to all active UI components.Correct
- c.Emitting a sequence of database records, where each subscriber needs to process the entire sequence from the start.
- d.Managing a single, mutable configuration object whose latest value is always available to new observers.
Why? this is the answer
SharedFlow is designed for one-to-many event distribution, like broadcasting UI events or real-time updates to multiple parts of an application, as described in option B. Option D describes the use case for StateFlow, which is specialized for a single, observable piece of state.
Just read this? Test yourself on what you have been reading.
Read the original → kotlinlang.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.
See open roles