Detecting that the peer's browser was closed in a webrtc videochat

Pedro Rolo picture Pedro Rolo · Jan 20, 2014 · Viewed 11.4k times · Source

I've been implementing a webrtc videochat.

Everything is working smoothly except for the case when the peer closes the browser.

I've been trying to handle this event by implementing an onended callback on the remote mediastream. Though, this callback does not seem to ever be called.

How can I detect that the peer's browser has been closed or that the connection was finished on the other side?

Answer

MarijnS95 picture MarijnS95 · Jan 22, 2014

You can use the ICE connection status to determine this. If you disconnect one peer it takes some seconds (~5?) to recoginize it, but it works even without a signalling server.

(assuming you called your peer connection pc)

pc.oniceconnectionstatechange = function() {
    if(pc.iceConnectionState == 'disconnected') {
        console.log('Disconnected');
    }
}