# Observer events and know if producers or consumers closed abruptly

**URL:** <https://mediasoup.discourse.group/t/observer-events-and-know-if-producers-or-consumers-closed-abruptly/2916>\
**Category:** mediasoup libraries\
**Created:** [June 10, 2021, 4:02pm UTC](https://mediasoup.discourse.group/t/observer-events-and-know-if-producers-or-consumers-closed-abruptly/2916 "2021-06-10T16:02:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![subham](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/subham/32/1760_2.png) [@subham](https://mediasoup.discourse.group/u/subham)\
**Post date:** [June 10, 2021, 4:02pm UTC](https://mediasoup.discourse.group/t/observer-events-and-know-if-producers-or-consumers-closed-abruptly/2916/1 "2021-06-10T16:02:48Z")

</div>

Hi,  
So I was trying to wrap my head around the question that do producers or consumers be disconnected/closed for whatever reasons even if the concerned transport is connected ?  
Looking into observer events we have,

> **[mediasoup :: API](https://mediasoup.org/documentation/v3/mediasoup-client/api/#producer-observer-on-close)**
>
> Cutting Edge WebRTC Video Conferencing

> **[mediasoup :: API](https://mediasoup.org/documentation/v3/mediasoup-client/api/#consumer-observer-on-close)**
>
> Cutting Edge WebRTC Video Conferencing

> **[mediasoup :: API](https://mediasoup.org/documentation/v3/mediasoup/api/#producer-observer-on-close)**
>
> Cutting Edge WebRTC Video Conferencing

> **[mediasoup :: API](https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-observer-on-close)**
>
> Cutting Edge WebRTC Video Conferencing

Now in Producer.ts and Consumers.ts in mediasoup-client and same for the mediasoup server side,  
I could see these events emitted on either close() or transportClosed() for producers/consumers, which we already have control over or related events (transpportclose) are already subscribed to. So I am assuming that there isn’t any other case where these observer events would be emitted. Hence no way to know for an abrupt closure using observer events.

I am trying to figure out if

1. Producers/Consumers will be closed without any events emitted (like an exception or failure) with the transport still connected ?
2. If such a scenario is possible how would we possibly know that the producer/consumer is alive even with a ping/pong mechanism between client server to know the status of the concerned producer/consumer ?

Thanks

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [June 10, 2021, 4:24pm UTC](https://mediasoup.discourse.group/t/observer-events-and-know-if-producers-or-consumers-closed-abruptly/2916/2 "2021-06-10T16:24:58Z")

</div>

> [@subham](#):
>
> Producers/Consumers will be closed without any events emitted (like an exception or failure) with the transport still connected ?

No. Once a Producer/Consumer is created they will emit a “xxxxxclose” event if they are closed due to their parent Transport closure (or “produceclose” in Consumer in case its associated Producer is closed).

BTW observers ar eintended for 3rd party libraries that do NOT call mediasoup API methods. In your app core logic you should not use observers (in theory). If you call `producer.close()` you know that you are closing it so no need for an event telling you “hey you have closed this”. However the Producer observer will always emit “close” no matter how it was closed.

---

<div class="post-metadata">

**Author:** ![subham](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/subham/32/1760_2.png) [@subham](https://mediasoup.discourse.group/u/subham)\
**Post date:** [June 10, 2021, 4:45pm UTC](https://mediasoup.discourse.group/t/observer-events-and-know-if-producers-or-consumers-closed-abruptly/2916/3 "2021-06-10T16:45:39Z")

</div>

Thanks for the reply.

> However the Producer observer will always emit “close” no matter how it was closed.

Will a producer/consumer be closed in any other circumstances even if their parent “transportclose” never occurs ( or “producerclose” never occurs in case of a consumer) ?  
(trying to understand if the observer will emit close in such cases and be used to know produce/consumer closure).

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [June 10, 2021, 4:54pm UTC](https://mediasoup.discourse.group/t/observer-events-and-know-if-producers-or-consumers-closed-abruptly/2916/4 "2021-06-10T16:54:08Z")

</div>

I promise that the API documentation specifies all the cases in which `xxxxx.observer.on('close')` is emitted.

---

<div class="post-metadata">

**Author:** ![subham](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/subham/32/1760_2.png) [@subham](https://mediasoup.discourse.group/u/subham)\
**Post date:** [June 10, 2021, 5:05pm UTC](https://mediasoup.discourse.group/t/observer-events-and-know-if-producers-or-consumers-closed-abruptly/2916/5 "2021-06-10T17:05:14Z")

</div>

Sorry and I apologize if I am being repetitive here.  
Here is my point of confusion (taking consumer as reference).

1. consumer.observer.on(“close”, fn()) Emitted when the consumer is closed for **whatever reason**
2. in mediasoup-client code, I can see **this.\_observer.safeEmit(‘close’);** from closed() (calling consumer.close()) and transportClosed()
3. I could not find any other cases where the close event would be emitted or if I have missed it somewhere in the documentation.

> I promise that the API documentation specifies all the cases in which `xxxxx.observer.on('close')` is emitted.

1. If so, I am not sure if I follow what all the cases mean or where do I find them in the documentation.

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [June 10, 2021, 5:52pm UTC](https://mediasoup.discourse.group/t/observer-events-and-know-if-producers-or-consumers-closed-abruptly/2916/6 "2021-06-10T17:52:53Z")

</div>

> [@subham](#):
>
> 1. consumer.observer.on(“close”, fn()) Emitted when the consumer is closed for **whatever reason**
> 2. in mediasoup-client code, I can see **this.\_observer.safeEmit(‘close’);** from closed() (calling consumer.close()) and transportClosed()
> 3. I could not find any other cases where the close event would be emitted or if I have missed it somewhere in the documentation.

By definition a Consumer is closed when its associated Producer is closed (for whatever reason). So when the Producer is closed, the Consumer will emit `producerclose` event and its observer will emit `close`.
