# What's the best to wrap a callback-based listener with coroutines?

**URL:** <https://discuss.kotlinlang.org/t/whats-the-best-to-wrap-a-callback-based-listener-with-coroutines/15588>\
**Category:** Libraries\
**Created:** [December 23, 2019, 8:34pm UTC](https://discuss.kotlinlang.org/t/whats-the-best-to-wrap-a-callback-based-listener-with-coroutines/15588 "2019-12-23T20:34:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [December 23, 2019, 8:34pm UTC](https://discuss.kotlinlang.org/t/whats-the-best-to-wrap-a-callback-based-listener-with-coroutines/15588/1 "2019-12-23T20:34:22Z")

</div>

Say you have a callback with some arguments that is called many times to notify an event. How would you translate it with coroutines? Is it better a `ReceiveCannel` or a `Flow`? How to implement both without some memory leak?

Here’s my usecase. I need to use Firebase for a project that is going multiplatform and I need a data structure available in common that is capable of handling callback registrations and removal.  
For example how would you handle with a `Flow` the [`authStateListener`](https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth.AuthStateListener)?

```kotlin
actual val authStateFlow: Flow<Boolean>
    get() = flow {
        supervisorScope {
            delegate.addAuthStateListener {
                launch { emit(it.currentUser != null) }
            }
        }
    }

```

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [December 24, 2019, 12:52am UTC](https://discuss.kotlinlang.org/t/whats-the-best-to-wrap-a-callback-based-listener-with-coroutines/15588/2 "2019-12-24T00:52:29Z")

</div>

See [callbackFlow](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/callback-flow.html). It’s for exactly this.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [December 24, 2019, 1:57am UTC](https://discuss.kotlinlang.org/t/whats-the-best-to-wrap-a-callback-based-listener-with-coroutines/15588/3 "2019-12-24T01:57:31Z")

</div>

🤩 Thank you! I was not aware of that!

---

<div class="post-metadata">

**Author:** ![tim4dev](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@tim4dev](https://discuss.kotlinlang.org/u/tim4dev)\
**Post date:** [December 31, 2019, 6:56am UTC](https://discuss.kotlinlang.org/t/whats-the-best-to-wrap-a-callback-based-listener-with-coroutines/15588/4 "2019-12-31T06:56:29Z")

</div>

[Callbacks and Kotlin Flows](https://medium.com/@elizarov/callbacks-and-kotlin-flows-2b53aa2525cf)
