WebRTC: Matching up nearest peers

Posted on

WebRTC: Matching up nearest peers

Given a single public IP address (peer A) and a list of many other public IP addresses (a mix of IPv4 and IPv6 addresses), what is the easiest way to match up peer A the IP addresses of the n nearest peers without having the peers manually ping each other for latency benchmarking?

I think that this is possible using BGP with a bunch of complicated queries (and maybe something involving OSPF), but I was hoping that there might be a solution or library that would make it as easy as the theoretical functional call below.

// `peer` is a single IP address. `peer_list` is a list of IP addresses
// get the 5 nearest peers (ordered) to `peer` from `peer_list`
nearest_peers = get_nearest_ips(peer, peer_list, 5);

Should I just use a local instance of MaxMind’s GeoIP database + Haversine/Vincenty, or is it practical to use BGP through a library (with proper caching where needed) to accomplish this?

It seems like this kind of code might exist in an open source anycast routing implementation, although I haven’t been able to find anything that fits this use case.

The solution or suggested library doesn’t have to work on node.js—any language is fine.

Solution :

Install https://github.com/runk/node-maxmind

Download ‘GeoLite2-City.mmdb’ from: http://dev.maxmind.com/geoip/geoip2/geolite2/

var maxmind = require('maxmind');
var lookup = maxmind.open('./GeoLite2-City.mmdb');

var peers = [
    '', // UK
    '', // USA
    '', // Turkey
    '', // Tunisia
    '' // Malta

var peerLocations = {};

peers.forEach(function(peer) {

    var tmp = lookup.get(peer);

    if (!tmp || !tmp.location) {
        throw new Error('Unable to get initial peer location: ' + peer);
    peerLocations[peer] = tmp.location;


var testIp = ''; // Turkey
//   // Turkey
//   // Finland
//     // France
//   // Malta
//     // UK
//     // USA
//   // Tunisia

console.log( findClosestPeer(testIp, 3) );

function findClosestPeer(ip, len) {

    var ipData = lookup.get(ip);
    var distances = [];

    if (ipData && ipData.location) {

        Object.keys(peerLocations).forEach(function(key) {

            var peer = peerLocations[key];
            var distance = getDistanceFromLatLonInKM(ipData.location.latitude, ipData.location.longitude, 
                peer.latitude, peer.longitude);

            distances.push({ip: key, distance: distance});

    // 0 ... 9
    distances.sort(function(a, b) {
        return a.distance - b.distance;

    return len > 1 ? distances.slice(0, len)
        : distances.shift();

/* http://stackoverflow.com/a/21279990/605399 */
function getDistanceFromLatLonInKM(lat1, lon1, lat2, lon2) {

    var R = 6371; // Radius of the earth in km

    var dLat = deg2rad(lat2 - lat1);  // deg2rad below
    var dLon = deg2rad(lon2 - lon1); 
    var a = 
        Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
        Math.sin(dLon/2) * Math.sin(dLon/2)

    var c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1 - a) ); 
    var d = R * c; // Distance in km

    return d;

function deg2rad(deg) {
    return deg * ( Math.PI / 180 );

As I read it, your question is way more general than your Javascript / WebRTC use case.

Who about something like: “Given a P2P network, and a central server that knows all connected peers, which is the best metric than can be used to pair them up?”.

=> As good metric to pair two arbitrary nodes would be the hop-distance between them. The problem being that this value is not possible to compute (you can only guess which path the ISP routers will chose between the nodes).

How to approximate it then?

1. Use geographical distance as an approximation to hop distance

In that case, you are pretty much done. Use any “ip to latlng” service and you are done.

2. Try to guess the real hop distance by mapping the internet

I found a paper on that subject, that might be useful to you. You might as well dig a bit on their references to retrieve previous papers on the same subject:

Estimating Hop Distance Between Arbitrary Host Pairs

Abstract — Establishing a clear and timely picture of Internet
topology is complicated by many factors including the vast size
and dynamic nature of the infrastructure. In this paper, we describe
a methodology for estimating an important characteristic
of Internet topology – the hop distance between arbitrary pairs
of end hosts. Our goal is to develop an approach to pairwise
hop distance estimation that is accurate, scalable, timely and
does not require a significant measurement infrastructure. Our
methodology is based on deploying a small set of landmark
nodes that use traceroute-like probes between each other to
establish a set of accurate pairwise hop distances. The landmark
nodes are also configured to collect source IP addresses and
TTL values from passively monitored network packet traffic.
We develop a novel multidimensional scaling algorithm that
can be applied to both the passive and active measurements to
generate pairwise hop distance estimates for all of the observed
source host addresses. The basic algorithm is then enhanced to
consider the autonomous system membership of source hosts via
BGP routing information. We investigate the capabilities of our
estimation algorithms using a set of synthetic network topologies.
The results show that our method can generate highly accurate
pairwise hop distance estimates over a range of network sizes
and configurations, and landmark infrastructure sizes.

The easiest way to find the nearest peers, would be to send each of the peers an echo request and measure the time it takes to get a response, like ping does.

Leave a Reply

Your email address will not be published. Required fields are marked *