When to clean up ARI recording event listeners

In my ARI application, I set up listeners on a recording for RecordingStarted, RecordingFinished, and RecordingFailed, and then start recording a bridge.

When either RecordingFinished or RecordingFailed is fired, or if the request to start recording the bridge fails, I want to clean up all the listeners I set up.

However, if the bridge is destroyed around the same time as setting up this recording, I get different behaviour depending on the exact timing:

  • No RecordingStarted or RecordingFinished event at all, even though the request to start recording the bridge succeeds
  • Just a RecordingStarted event, but no RecordingFinished event
  • A RecordingStarted event followed by a RecordingFinished event (good)

In the first two cases, I don’t know when to clean up the listeners on the recording - in the first case there is no event at all, and in the second case I can’t tell this apart from a situation where the recording has actually started and I obviously don’t want to clean up the listeners.

Has anyone else dealt with a similar issue or have any pointers?

In case it helps anyone else, the best workaround I have found so far is to use whatever comes first out of RecordingFinished on the recording and BridgeDestroyed on the bridge when recording a bridge (using Promise.race() in JS).

I encountered the same problem with PlaybackFinished events when playing media to a bridge, and again decided to use whatever comes first out of PlaybackFinished on the playback and BridgeDestroyed on the bridge in that case.