By jmarbach
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?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
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
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()
}
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.