Background
I am building a Ruby on Rails application that lets users deploy a server in the DO region of their choice.

Problem
I’d like to show the latency between the user’s location and all the available DO regions so that the user can select the region with the fastest response time for them.

The latency can be easily calculated locally in development for each region with Net::Ping using the example code below:

  check = Net::Ping::External.new(example_region_ip_address)
  start_time = Time.now
  check.ping?
  elapsed_time = ('%.6f' % (Time.now - start_time))

However, in production, the originating IP is consistently the web host of my application, not the user’s IP address.

How can I measure the response time between a website visitor and a DO region?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

Thanks for taking a look @bobbyiliev.

I was able to solve this problem by using JavaScript to ping a website hosted in each of DO’s regions. I used the speed test websites originally designed to help DO customers measure the upload/download speed from each region.

For example, the code below will ping a website hosted in each DO region and then print the response time latency in the console.

var region_data = [
  {"name":"New York","host":"http://speedtest-nyc1.digitalocean.com"}, 
  {"name":"Toronto","host":"http://speedtest-tor1.digitalocean.com"},
  {"name":"San Francisco","host":"http://speedtest-sfo1.digitalocean.com"}, 
  {"name":"London","host":"http://speedtest-lon1.digitalocean.com"}, 
  {"name":"Amsterdam","host":"http://speedtest-ams2.digitalocean.com"}, 
  {"name":"Frankfurt","host":"http://speedtest-fra1.digitalocean.com"}, 
  {"name":"Singapore","host":"http://speedtest-sgp1.digitalocean.com"}, 
  {"name":"Bangalore","host":"http://speedtest-blr1.digitalocean.com"}
];

for (var i = 0; i < region_data.length; i++) {
  var xmlhttp = new XMLHttpRequest()
  var start_time = Date.now();
  xmlhttp.open('GET', region_data[i].host, false)
  xmlhttp.onload = function () {
    var elapsed_time = Date.now() - start_time;
    console.log(region_data[i].name + ': ' + elapsed_time);
  }

  // Send request
  xmlhttp.send()
}

Hi there @jmarbach,

Usually you would be seeing the server IP address in case that you are using Nginx as a reverse proxy. To get the real IP address of the visitor you have to get the X-forwarded-for header value. That way you would get the actual IP rather than the server IP itself.

Hope that this helps!
Regards,
Bobby

Submit an Answer