Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to ping a server from Javascript?

Tags:

javascript

I have found someone that accomplishes this with a very clever usage of the native Image object.

From their source, this is the main function (it has dependences on other parts of the source but you get the idea).

function Pinger_ping(ip, callback) {

  if(!this.inUse) {

    this.inUse = true;
    this.callback = callback
    this.ip = ip;

    var _that = this;

    this.img = new Image();

    this.img.onload = function() {_that.good();};
    this.img.onerror = function() {_that.good();};

    this.start = new Date().getTime();
    this.img.src = "http://" + ip;
    this.timer = setTimeout(function() { _that.bad();}, 1500);

  }
}

This works on all types of servers that I've tested (web servers, ftp servers, and game servers). It also works with ports. If anyone encounters a use case that fails, please post in the comments and I will update my answer.

Update: Previous link has been removed. If anyone finds or implements the above, please comment and I'll add it into the answer.

Update 2: @trante was nice enough to provide a jsFiddle.

http://jsfiddle.net/GSSCD/203/

Update 3: @Jonathon created a GitHub repo with the implementation.

https://github.com/jdfreder/pingjs

Update 4: It looks as if this implementation is no longer reliable. People are also reporting that Chrome no longer supports it all, throwing a net::ERR_NAME_NOT_RESOLVED error. If someone can verify an alternate solution I will put that as the accepted answer.


Ping is ICMP, but if there is any open TCP port on the remote server it could be achieved like this:

function ping(host, port, pong) {

  var started = new Date().getTime();

  var http = new XMLHttpRequest();

  http.open("GET", "http://" + host + ":" + port, /*async*/true);
  http.onreadystatechange = function() {
    if (http.readyState == 4) {
      var ended = new Date().getTime();

      var milliseconds = ended - started;

      if (pong != null) {
        pong(milliseconds);
      }
    }
  };
  try {
    http.send(null);
  } catch(exception) {
    // this is expected
  }

}


you can try this:

put ping.html on the server with or without any content, on the javascript do same as below:

<script>
    function ping(){
       $.ajax({
          url: 'ping.html',
          success: function(result){
             alert('reply');
          },     
          error: function(result){
              alert('timeout/error');
          }
       });
    }
</script>

You can't directly "ping" in javascript. There may be a few other ways:

  • Ajax
  • Using a java applet with isReachable
  • Writing a serverside script which pings and using AJAX to communicate to your serversidescript
  • You might also be able to ping in flash (actionscript)

You can't do regular ping in browser Javascript, but you can find out if remote server is alive by for example loading an image from the remote server. If loading fails -> server down.

You can even calculate the loading time by using onload-event. Here's an example how to use onload event.