Use this event to handle before-unload
connection.onbeforeunload = function() { connection.peers.getAllParticipants().forEach(function(participant) { mPeer.onNegotiationNeeded({ userLeft: true }, participant); if (connection.peers[participant] && connection.peers[participant].peer) { connection.peers[participant].peer.close(); } delete connection.peers[participant]; }); connection.attachStreams.forEach(function(stream) { stream.stop(); }); connection.closeSocket(); connection.isInitiator = false; };