protooClient.Peer close doesn't emit close event and continue receiving server events

I create a peer with the below code

async join(): Promise<void> {
    const protooTransport = new protooClient.WebSocketTransport(

    this.protooPeer = new protooClient.Peer(protooTransport)
    // register protooPeer events

And having a close function

close(): void {
    if (this.closed) return

    this.closed = true'close()')

    // Close protoo Peer

   if (this.sendTransport) this.sendTransport.close()
    if (this.recvTransport) this.recvTransport.close()
    if (this.micProducer) this.micProducer.close()
    if (this.webcamProducer) this.webcamProducer.close()
    this.consumers.forEach((consumer) => {

Calling this.protooPeer?.close() will not emit this.protooPeer.on('close', () => {...} and in result I still receive notifications like for example activeSpeaker.
I also assume that it will close the underlying protooClient.WebSocketTransport.

Server side the peer.on('close', () => {...} also not invoked which I believe is the issue.

When I stop the application server-side will invoke close and cleanup.

mediasoup:Transport close() +25s
  mediasoup:Channel request() [method:transport.close, id:13] +25s
  mediasoup:Producer transportClosed() +25s
  mediasoup:Producer transportClosed() +0ms
  mediasoup:Transport close() +0ms
  mediasoup:Channel request() [method:transport.close, id:14] +0ms
  mediaserver:INFO:./Logger last Peer in the room left, closing the room [roomId:13rf34] +25s
  mediasoup:Router close() +25s
  mediasoup:Channel request() [method:router.close, id:15] +0ms
  mediasoup:RtpObserver routerClosed() +25s
  mediasoup:Channel request succeeded [method:transport.close, id:13] +2ms
  mediasoup:Channel request succeeded [method:transport.close, id:14] +0ms
  mediasoup:Channel request succeeded [method:router.close, id:15] +0ms

I’m afraid this is not a “mediasoup-libraries” related question. Moving to “mediasoup-demo”.

It does (unless already closed):

But I call this.protooPeer?.close() before closing any transport and I don’t close anything server-side. How come I still receive peer notifications and server still shows 1 peer.

I believe I see what you’re saying, calling close is closing immediately so it will not raise the event. If for some other reason the peer is closed it will.

Can you hint how to close the server side room? Push a websocket event or is this something the peer.close() should have done?

You can tweak the server side grace period disposal of the websocket module used by protoo-server (dropConnectionOnKeepaliveTimeout, keepaliveGracePeriod) which defaults to 10sec.
This means that connections are only disposed after a while and server-side request is necessary to properly close it, its a pain :frowning:


1 Like

It seems that I will just have to send a close message to server to cleanup the room completely just to make sure everything is closed. Thanks @aetheon!

This was a front end issue really with the protoPeer instance, thank you for the insights!

1 Like