Ok, taken from my answer to another question
I wrote and tested the code below, it works in recent versions of both firefox and chrome, getConnectionDetails returns a promise that resolves the connection details:
function getConnectionDetails(peerConnection){ var connectionDetails = {}; // the final result object. if(window.chrome){ // checking if chrome var reqFields = [ 'googLocalAddress', 'googLocalCandidateType', 'googRemoteAddress', 'googRemoteCandidateType' ]; return new Promise(function(resolve, reject){ peerConnection.getStats(function(stats){ var filtered = stats.result().filter(function(e){return e.id.indexOf('Conn-audio')==0 && e.stat('googActiveConnection')=='true'})[0]; if(!filtered) return reject('Something is wrong...'); reqFields.forEach(function(e){connectionDetails[e.replace('goog', '')] = filtered.stat(e)}); resolve(connectionDetails); }); }); }else{ // assuming it is firefox return peerConnection.getStats(null).then(function(stats){ var selectedCandidatePair = stats[Object.keys(stats).filter(function(key){return stats[key].selected})[0]] , localICE = stats[selectedCandidatePair.localCandidateId] , remoteICE = stats[selectedCandidatePair.remoteCandidateId]; connectionDetails.LocalAddress = [localICE.ipAddress, localICE.portNumber].join(':'); connectionDetails.RemoteAddress = [remoteICE.ipAddress, remoteICE.portNumber].join(':'); connectionDetails.LocalCandidateType = localICE.candidateType; connectionDetails.RemoteCandidateType = remoteICE.candidateType; return connectionDetails; }); } } //usage example: getConnectionDetails(pc).then(console.log.bind(console));
source share