0
0
Kotlinprogramming~10 mins

SharedFlow for event broadcasting in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SharedFlow for event broadcasting
Create SharedFlow
Emit Event
SharedFlow holds event
Subscribers collect event
Subscribers react to event
Wait for next event or complete
SharedFlow lets multiple parts listen to events. When an event is sent, all listeners get it.
Execution Sample
Kotlin
val events = MutableSharedFlow<String>(replay = 1)

// Emit event
launch { events.emit("Hello") }

// Collect event
launch { events.collect { println(it) } }
This code creates a SharedFlow, sends a "Hello" event, and prints it when collected.
Execution Table
StepActionSharedFlow StateSubscribersOutput
1Create MutableSharedFlowEmptyNone
2Launch emitter coroutineEmptyNone
3Emit "Hello"Contains "Hello"None
4Launch collector coroutineContains "Hello"Collector started
5Collector receives "Hello"Contains "Hello"Collector activePrint: Hello
6Wait for next eventContains "Hello"Collector active
💡 No more events emitted, collector waits for new events
Variable Tracker
VariableStartAfter Step 3After Step 5Final
eventsEmpty SharedFlowContains "Hello"Contains "Hello"Contains "Hello"
SubscribersNoneNoneCollector activeCollector active
Key Moments - 2 Insights
Why does the collector receive the event even though it started after the event was emitted?
Because SharedFlow can replay events to new subscribers if replay is set. Here, the event was still in the buffer when the collector started (see step 4 and 5 in execution_table).
What happens if no subscriber is active when an event is emitted?
The event stays in SharedFlow if replay > 0, so future subscribers can get it. Otherwise, emit suspends until subscriber starts. In this example, the event was held until the collector started (step 3 to 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the SharedFlow state right after emitting "Hello"?
AContains "Hello"
BEmpty
CContains "Hello" and collector active
DCollector active only
💡 Hint
Check step 3 in the execution_table under SharedFlow State
At which step does the collector print the event?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the Output column in execution_table
If the SharedFlow had replay = 0, what would happen when the collector starts after the event is emitted?
ACollector receives the event anyway
BCollector misses the event
CEvent is duplicated
DProgram crashes
💡 Hint
With replay=0, emit() suspends until collector joins; see key_moments
Concept Snapshot
SharedFlow broadcasts events to many listeners.
Create with MutableSharedFlow.
Emit events with emit().
Subscribers collect events with collect().
Replay controls if new subscribers get past events.
Useful for one-to-many event communication.
Full Transcript
SharedFlow is a Kotlin tool to send events to many listeners. First, you create a MutableSharedFlow. Then, you emit events into it. These events stay in the flow depending on replay settings. When subscribers start collecting, they receive events currently in the flow. In the example, an event "Hello" is emitted before the collector starts. Because the event is still in the flow, the collector receives and prints it. If replay was zero, the collector would miss past events. SharedFlow is great for broadcasting events like button clicks or messages to multiple parts of your app.