Skip to main content

SharedFlow in Kotlin

2 min read
Share:
On this page (12sections)

Introduction

SharedFlow is a fundamental concept every Kotlin developer should understand. Flow streams values over time, similar to RxJava observables but with structured concurrency and coroutine integration.

SharedFlow emits events to multiple collectors. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • SharedFlow emits events to multiple collectors.
  • It can replay recent values based on configuration.
  • Useful for one-time events and broadcasts.

Syntax

MutableSharedFlow()

SharedFlow in Kotlin Example Program in Kotlin

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main(args: Array<String>) = runBlocking {
    val events = MutableSharedFlow<String>(replay = 1)
    val job = launch {
        events.collect { println("Received: $it") }
    }
    events.emit("Updated")
    job.cancel()
}

Sample Output

Received: Updated

When to use

Use Flow for streams of values over time — sensor readings, paginated API pages, or search-as-you-type results.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. val events = MutableSharedFlow<String>(replay = 1) assigns or updates a value used later in the program.

  3. val job = launch { assigns or updates a value used later in the program.

  4. The println("Received: $it") statement writes a line to the console — this produces part of the sample output below.

  5. SharedFlow emits events to multiple collectors.

  6. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

  • Understand the core idea: sharedFlow emits events to multiple collectors.
  • Prefer readable names and small functions so examples map directly to real projects.
  • Run and modify the example — change values and observe how the output changes.

Common Mistakes

  • Skipping the example and only reading the definition — hands-on practice cements the concept.
  • Copying syntax without understanding nullable vs non-nullable types or scope rules.
  • Ignoring compiler warnings that often point to safer alternatives.

Key Points

  • SharedFlow emits events to multiple collectors.
  • It can replay recent values based on configuration.
  • Useful for one-time events and broadcasts.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Add the kotlinx-coroutines-core dependency when running coroutine examples outside Android or IntelliJ.
  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is SharedFlow in Kotlin?
SharedFlow emits events to multiple collectors.
When should I use SharedFlow?
Use Flow for streams of values over time — sensor readings, paginated API pages, or search-as-you-type results.
How is SharedFlow different from Java?
Useful for one-time events and broadcasts.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related Tutorials

Search tutorials