I have written a code which tells me the time taken by a packet to reach from server to client
and also total time taken from client to server to client
again. This is my code.
CLIENT SIDE:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Ping</title>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function(e) {
var socket = io.connect('http://pingme.jit.su:80/', {secure: false});
$('#button').click(function(e) {
e.preventDefault();
$(this).attr('disabled','disabled');
var time = (new Date()).getTime();
socket.emit('ping', time);
});
socket.on('pong', function(data) {
var time2 = (new Date()).getTime();
var lat = time2 - data.server;
var roundtrip = time2 - data.init;
var str = '<br><br><strong>From Server</strong>: '+lat+' ms<br><strong>Roundtrip</strong>: '+roundtrip+' ms<br><br>';
$('#res').prepend(str);
$('#button').removeAttr('disabled');
});
});
</script>
</head>
<body style="margin:0;">
<input type="button" name="Button" id="button" value="Latency">
<div id="res"></div>
</body>
</html>
SERVER SIDE:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
, path = require('path')
, url = require("url")
, querystring = require("querystring");
app.listen(8080);
io.configure('development', function(){
io.set('transports', ['xhr-polling']);
});
io.configure('production', function(){
io.set('transports', ['xhr-polling']);
});
var static = require('node-static');
var fileServer = new static.Server('.', { cache: false });
function handler (request, response) {
var pathname = url.parse(request.url).pathname;
if(pathname == '/') {
pathname = '/app.html';
}
request.addListener('end', function () {
fileServer.serveFile(pathname, 200, {}, request, response, function(err, result) {
if(err) {
console.log(err);
console.log(err.status);
}
else
console.log(result);
});
});
}
io.sockets.on('connection', function (socket) {
socket.on('ping',function(data) {
var time = (new Date()).getTime();
socket.emit('pong', {server:time, init:data});
});
});
The problem while this works well locally showing the following output:
From Server: 4 ms
Roundtrip: 11 ms
From Server: 10 ms
Roundtrip: 15 ms
I get abnormal results when I run after deploying this on Nodejitsu. It gives me following output:
From Server: 2223 ms
Roundtrip: 956 ms
From Server: 2265 ms
Roundtrip: 915 ms
How it is this possible that packet takes more time to travel from server than the whole roundtrip? I think it is due to the difference of time between server and client. What do you think it is?
There are lots of reasons why this happens. but if you want to test your programming then you should compare the results to something like traceroute (command on linux).
Things are always going to be WAYYY quicker locally. When you access something outside of your LAN you are going to get all sorts of overheads and latency.
A simple traceroute will probably explain a lot. Also, you can do it from here:
//EDIT//
There are lots of ways you can do this, check this link:
Latency / Ping test from Browser
But the underlying principal, is that you send multiple messages. You can use your first request-response messages to work out the time. I do this with about 1000 clients, what I do is keep track of the offest for each one. Once you calculate the offset (i.e. the client is 1 hour ahead of the server), then you can minus it from your latency calculations. Check out this php function
http://php.net/manual/en/function.timezone-offset-get.php
That should at least help point you in the right direction :) - let me know if you need more help.