A tiny JavaScript library using WebRTC getStats API to return peer connection stats i.e. bandwidth usage, packets lost, local/remote ip addresses and ports, type of connection etc.
It is MIT Licenced, which means that you can use it in any commercial/non-commercial product, free of cost.
npm install getstats
cd node_modules
cd getstats
node server.js
# and open:
# http://localhost:9999/
To use it:
<script src="./node_modules/getstats/getStats.js"></script><script src="https://cdn.webrtc-experiment.com/getStats.js"></script>Or link specific build:
<script src="https://github.com/muaz-khan/getStats/releases/download/1.0.4/getStats.js"></script>To invoke directly:
getStats(peer, callback, interval);Or, to setup an instance method:
// if your code is encapsulated under a method
(function() {
RTCPeerConnection.prototype.getPeerStats = window.getStats;
// or
RTCPeerConnection.prototype.__getStats = window.getStats;
// or
RTCPeerConnection.prototype.getConnectionStats = window.getStats;
// or
RTCPeerConnection.prototype['your-choice'] = window.getStats;
})();NEVER set/override RTCPeerConnection.prototype.getStats because it is a reserved method.
// following will fail
RTCPeerConnection.prototype.getStats = window.getStats;
// it should be
RTCPeerConnection.prototype.intanceMethodNamae = window.getStats;var rtcPeerConnection = new RTCPeerConnection(rtcConfig);
var repeatInterval = 2000; // 2000 ms == 2 seconds
rtcPeerConnection.getPeerStats(function(result) {
result.connectionType.remote.ipAddress
result.connectionType.remote.candidateType
result.connectionType.transport
result.audio.availableBandwidth
result.audio.packetsSent
result.audio.packetsLost
result.audio.rtt
// to access native "results" array
result.results.forEach(function(r) {
console.log(r);
});
}, repeatInterval);peer.getStats(peer.getLocalStreams()[0].getAudioTracks()[0], function(results) {
// rest goes here
}, 5 * 1000);// states => open or close
alert(result.datachannel.state === 'open');Offerer is the person who invoked createOffer method.
To detect which tech is used to encrypt your connections.
alert(result.encryption === 'sha-256');This function can be used to ask to stop invoking getStats API.
btnStopGetStats.onclick = function() {
getStatsResult.nomore();
};result.audio.availableBandwidthresult.audio.inputLevelresult.audio.packetsLostresult.audio.rttresult.audio.packetsSentresult.audio.bytesSent
result.video.availableBandwidthresult.video.googFrameHeightInputresult.video.googFrameWidthInputresult.video.googCaptureQueueDelayMsPerSresult.video.rttresult.video.packetsLostresult.video.packetsSentresult.video.googEncodeUsagePercentresult.video.googCpuLimitedResolutionresult.video.googNacksReceivedresult.video.googFrameRateInputresult.video.googPlisReceivedresult.video.googViewLimitedResolutionresult.video.googCaptureJitterMsresult.video.googAvgEncodeMsresult.video.googFrameHeightSentresult.video.googFrameRateSentresult.video.googBandwidthLimitedResolutionresult.video.googFrameWidthSentresult.video.googFirsReceivedresult.video.bytesSent
result.connectionType.local.candidateTyperesult.connectionType.local.ipAddressresult.connectionType.local.networkTyperesult.connectionType.remote.candidateTyperesult.connectionType.remote.ipAddressresult.connectionType.transport
It is an array that is returned by browser's native PeerConnection API.
console.log(result.results);getStats.js is released under MIT licence . Copyright (c) Muaz Khan.