Detect when Flow is connected to messaging system

Hi,

I’m creating a Flow with events from messaging system (Solace in my case, but it’s similar to Kafka or Rabbit).

I have val fl: Flow<Msg> which connects to the messaging system after user starts collecting it. So it is a cold flow and two parallel collections result in two connections to the messaging system. And when the user stops collecting the flow it disconnects from the messaging system.

Sometimes I would like to know when it’s connected (because I want to send a request and receive the reply through the flow - if I send request to early before it’s connected I can miss the reply). For this purpose Flow.onStart() doesn’t work because it’s called before collecting the flow.

I wonder how to detect when flow is connected - if possible without changing type of messages inside flow (eg. to Flow<Event>)?

Thanks

SharedFlow has onSubscription.
The provided action is called from inside collect so any emitted items will definitely be received (unlike onStart) but that doesn’t guarantee that Solace is setup.

Unless you build it in specifically, there’s no way to know when the underlying messaging system is hooked up.

2 Likes