# Need some suggestions about events with coroutines and flows

**URL:** <https://discuss.kotlinlang.org/t/need-some-suggestions-about-events-with-coroutines-and-flows/21000>\
**Category:** Support\
**Created:** [February 27, 2021, 10:12pm UTC](https://discuss.kotlinlang.org/t/need-some-suggestions-about-events-with-coroutines-and-flows/21000 "2021-02-27T22:12:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![crg7475](https://avatars.discourse-cdn.com/v4/letter/c/4bbf92/32.png) [@crg7475](https://discuss.kotlinlang.org/u/crg7475)\
**Post date:** [February 27, 2021, 10:12pm UTC](https://discuss.kotlinlang.org/t/need-some-suggestions-about-events-with-coroutines-and-flows/21000/1 "2021-02-27T22:12:06Z")

</div>

In my project, some events can happen that callers need to be able to handle. One example would be an event that a device was added to the system, and the new device’s ID is communicated when that event occurs. The classic approach would be to either have some sort of `onNewDevice` function for installing a callback that is invoked when a new device is added (and that callback gets passed the device’s ID), or perhaps some “startEvents” function with one of the arguments being such a callback.

However, I figure that if I am already using coroutines, then there’s gotta be a nicer way to do that. I did stumble upon callbackFlow, but this is not really what I need I think - if I got this correctly, callbackFlow is intended to integrate _existing_ callback-based APIs as flows.

So, how would you design an API to allow for notifying the user about a newly added device?

The one idea I had was to add a SharedFlow that transmits an Event structure that contains details about said event. However, with this flow, events _can_ get lost if the replay cache gets full. I’d rather retain the backpressure and block, since losing events would be bad.

Another idea would be a Channel that transmits Event structures.

What would you do?

---

<div class="post-metadata">

**Author:** ![mtimmerm](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mtimmerm/32/7160_2.png) [@mtimmerm](https://discuss.kotlinlang.org/u/mtimmerm)\
**Post date:** [February 28, 2021, 4:15pm UTC](https://discuss.kotlinlang.org/t/need-some-suggestions-about-events-with-coroutines-and-flows/21000/2 "2021-02-28T16:15:04Z")

</div>

The listener API is simple, sufficient, and well-understood. There’s no reason to provide anything more complicated.

If the caller wants to, it can build any sort of asynchronous coroutine-based receiver on top of the listener API. If you find that the standard library doesn’t make whatever you want easy, then you build a reusable class to implement what you want on top of _any_ listener API, instead of pushing complex requirements into the observable implementations.

---

<div class="post-metadata">

**Author:** ![crg7475](https://avatars.discourse-cdn.com/v4/letter/c/4bbf92/32.png) [@crg7475](https://discuss.kotlinlang.org/u/crg7475)\
**Post date:** [February 28, 2021, 6:29pm UTC](https://discuss.kotlinlang.org/t/need-some-suggestions-about-events-with-coroutines-and-flows/21000/3 "2021-02-28T18:29:37Z")

</div>

Right. Well, I’m still rather new to Kotlin, and it was my impression that using callbacks is not really idiomatic. That’s my reason for asking. But if it is perfectly fine, then I’ll stick to those.
