Skip to content
Advertisement

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

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?

Advertisement

Answer

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');
    }
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement