# Are \`close\` events in \`mediasoup-client\` replicated from server side?

**URL:** <https://mediasoup.discourse.group/t/are-close-events-in-mediasoup-client-replicated-from-server-side/3881>\
**Category:** mediasoup libraries\
**Created:** [January 28, 2022, 6:07pm UTC](https://mediasoup.discourse.group/t/are-close-events-in-mediasoup-client-replicated-from-server-side/3881 "2022-01-28T18:07:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![piranna](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/piranna/32/934_2.png) [@piranna](https://mediasoup.discourse.group/u/piranna)\
**Post date:** [January 28, 2022, 6:07pm UTC](https://mediasoup.discourse.group/t/are-close-events-in-mediasoup-client-replicated-from-server-side/3881/1 "2022-01-28T18:07:20Z")

</div>

On [Mediasoup-client documentation](https://mediasoup.org/documentation/v3/mediasoup-client/api/), it’s being shown multiple `close` events in all objects, including [Device](https://mediasoup.org/documentation/v3/mediasoup-client/api/#router-observer-on-close) or [Transport](https://mediasoup.org/documentation/v3/mediasoup-client/api/#transport-observer-on-close), where it’s said explicitly

> Emitted when [object] is closed for whatever reason.

This implies that “whatever reason” means also when closed on server side, so they are being propagated to client side. But on [mediasoup :: Communication Between Client and Server](https://mediasoup.org/documentation/v3/communication-between-client-and-server/#communicating-actions-and-events), it’s being specified that the application should signal the `close`, `pause`, `resume` and `layerschange` events. For the latest ones I somewhat understand it, but for `close` event, I assumed it would be send implicitly inwire because the connection socket gets closed, but [reading on the source code](https://github.com/versatica/mediasoup-client/blob/v3/src/Transport.ts) there’s nothing that notifies `Transport` objects that they got closed server side, and for `Producer` and `Consumer` they are just only being called when closed on the `Transport`. More annoying is on [Device object](https://github.com/versatica/mediasoup-client/blob/v3/src/Device.ts), where `close` event seems to not being emitted at all (maybe I’m looking on the wrong place) although [being documented](https://mediasoup.org/documentation/v3/mediasoup-client/api/#router-observer-on-close), so I want to ask:

- Is the documentation of Mediasoup client API wrong on this topic?
- Are Mediasoup client events only emitted due to calls on the client-side objects?
- Does I need to propagate the server side events myself? Is there no alternative?
- Could some events like `close` being detected and emitted implicitly by the library? Or is there a technological limitation on RTP that doesn’t allow to do it and we need to use a side channel for them? If not, why are they not propagated?

---

<div class="post-metadata">

**Author:** ![nazar-pc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/nazar-pc/32/369_2.png) [@nazar-pc](https://mediasoup.discourse.group/u/nazar-pc)\
**Post date:** [January 28, 2022, 7:24pm UTC](https://mediasoup.discourse.group/t/are-close-events-in-mediasoup-client-replicated-from-server-side/3881/2 "2022-01-28T19:24:59Z")

</div>

> [@piranna](#):
>
> so they are being propagated to client side

No, that is a responsibility of the signaling layer and mediasoup doesn’t have any out of the box.

---

<div class="post-metadata">

**Author:** ![snnz](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/snnz/32/693_2.png) [@snnz](https://mediasoup.discourse.group/u/snnz)\
**Post date:** [January 28, 2022, 8:28pm UTC](https://mediasoup.discourse.group/t/are-close-events-in-mediasoup-client-replicated-from-server-side/3881/3 "2022-01-28T20:28:06Z")

</div>

> [@piranna](#):
>
> Or is there a technological limitation on RTP that doesn’t allow to do it and we need to use a side channel

It’s not a limitation, but the design of WebRTC.

---

<div class="post-metadata">

**Author:** ![piranna](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/piranna/32/934_2.png) [@piranna](https://mediasoup.discourse.group/u/piranna)\
**Post date:** [January 31, 2022, 8:00am UTC](https://mediasoup.discourse.group/t/are-close-events-in-mediasoup-client-replicated-from-server-side/3881/4 "2022-01-31T08:00:15Z")

</div>

> [@nazar-pc](#):
>
> No, that is a responsibility of the signaling layer and mediasoup doesn’t have any out of the box.

> [@snnz](#):
>
> It’s not a limitation, but the design of WebRTC.

Then I misunderstood the Mediasoup documentation, I though `close` event would be automatically emited at least on lost connections too. Is this something that could / should be implemented?

---

<div class="post-metadata">

**Author:** ![snnz](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/snnz/32/693_2.png) [@snnz](https://mediasoup.discourse.group/u/snnz)\
**Post date:** [January 31, 2022, 11:11am UTC](https://mediasoup.discourse.group/t/are-close-events-in-mediasoup-client-replicated-from-server-side/3881/5 "2022-01-31T11:11:23Z")

</div>

By the way, it’s rather strange that a ‘close’ event is mentioned there for the observer of the Device class, because there is no such a thing. This observer emits only one event - ‘newtransport’.

---

<div class="post-metadata">

**Author:** ![nazar-pc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/nazar-pc/32/369_2.png) [@nazar-pc](https://mediasoup.discourse.group/u/nazar-pc)\
**Post date:** [January 31, 2022, 11:14am UTC](https://mediasoup.discourse.group/t/are-close-events-in-mediasoup-client-replicated-from-server-side/3881/6 "2022-01-31T11:14:22Z")

</div>

> [@piranna](#):
>
> Is this something that could / should be implemented?

No, that is a responsibility of the signaling layer and mediasoup doesn’t have any out of the box.

---

<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:** [January 31, 2022, 11:28am UTC](https://mediasoup.discourse.group/t/are-close-events-in-mediasoup-client-replicated-from-server-side/3881/7 "2022-01-31T11:28:29Z")

</div>

> [@piranna](#):
>
> Then I misunderstood the Mediasoup documentation, I though `close` event would be automatically emited at least on lost connections too. Is this something that could / should be implemented?

mediasoup is a ICE Lite server so it doesn’t send ICE Binding requests nor it expects ICE responses for them from endpoints, so there is no way for mediasoup to realize of ICE/network disconnections. This is not accurate since if the ICE goes over TCP then mediasoup does emit “disconnected” on WebRtcTransport, but that’s an exception due to the fact that in TCP we know that. In UDP we don’t. You can, at app level, establish a DataChannel connection between endpoints and mediasoup and make you mediasoup app send “PING” as DC messages to endpoints and assume the transport is closed if “PONG” is not replied by the endpoint after N seconds.

---

<div class="post-metadata">

**Author:** ![piranna](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/piranna/32/934_2.png) [@piranna](https://mediasoup.discourse.group/u/piranna)\
**Post date:** [January 31, 2022, 3:20pm UTC](https://mediasoup.discourse.group/t/are-close-events-in-mediasoup-client-replicated-from-server-side/3881/8 "2022-01-31T15:20:55Z")

</div>

> [@snnz](#):
>
> By the way, it’s rather strange that a ‘close’ event is mentioned there for the observer of the Device class, because there is no such a thing. This observer emits only one event - ‘newtransport’.

I just came to say just this, it’s in the docs but there’s no actual implementation.

---

<div class="post-metadata">

**Author:** ![piranna](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/piranna/32/934_2.png) [@piranna](https://mediasoup.discourse.group/u/piranna)\
**Post date:** [January 31, 2022, 3:26pm UTC](https://mediasoup.discourse.group/t/are-close-events-in-mediasoup-client-replicated-from-server-side/3881/9 "2022-01-31T15:26:58Z")

</div>

> [@ibc](#):
>
> This is not accurate since if the ICE goes over TCP then mediasoup does emit “disconnected” on WebRtcTransport, but that’s an exception due to the fact that in TCP we know that. In UDP we don’t.

Good point, didn’t though about UDP, I’m too much used to TCP 🙂

> [@ibc](#):
>
> You can, at app level, establish a DataChannel connection between endpoints and mediasoup and make you mediasoup app send “PING” as DC messages to endpoints and assume the transport is closed if “PONG” is not replied by the endpoint after N seconds.

Sound hack-ish, but I like it. Does DataChannels / SCTP connections protocol a ping message the same way as WebSockets has, or must it be implemented at application level too?

---

<div class="post-metadata">

**Author:** ![snnz](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/snnz/32/693_2.png) [@snnz](https://mediasoup.discourse.group/u/snnz)\
**Post date:** [January 31, 2022, 6:37pm UTC](https://mediasoup.discourse.group/t/are-close-events-in-mediasoup-client-replicated-from-server-side/3881/10 "2022-01-31T18:37:57Z")

</div>

> [@piranna](#):
>
> it’s in the docs but there’s no actual implementation.

The Device class is just a holder of the capabilities data, which is used to create the transports. It does not “connect” anywhere itself, so it is not even right to say that the ‘close’ event is not implemented, because there is nothing to implement.

---

<div class="post-metadata">

**Author:** ![piranna](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/piranna/32/934_2.png) [@piranna](https://mediasoup.discourse.group/u/piranna)\
**Post date:** [January 31, 2022, 6:40pm UTC](https://mediasoup.discourse.group/t/are-close-events-in-mediasoup-client-replicated-from-server-side/3881/11 "2022-01-31T18:40:45Z")

</div>

That’s the reason why I created the issue [Device `close` event doesn't exists · Issue #40 · versatica/mediasoup-website · GitHub](https://github.com/versatica/mediasoup-website/issues/40) 🙂
