1

How to detect if the server shut down during the client has visited a page with javascript?

A request with a forever keep-alive header could do it, but the problem is that fires even when the user navigates away from the page.

Andrew Barber
  • 37,547
  • 20
  • 91
  • 118
Adam Halasz
  • 51,803
  • 63
  • 138
  • 208
  • 3
    because it's not there any more?..... Why would a server shutdown? Surely that's a rare event, right? That's why we have clusters, UPS's and redundant drives... – Mitch Wheat Feb 13 '13 at 11:55
  • 3
    You may do an ajax request and see if it fails. – Denys Séguret Feb 13 '13 at 11:56
  • 1
    http://stackoverflow.com/questions/4282151/is-it-possible-to-ping-a-server-from-javascript – l2aelba Feb 13 '13 at 11:57
  • I guess that by "server is down" you actually mean "server is not responsive". Making any fake AJAX call with timeout will do. – freakish Feb 13 '13 at 12:00
  • you can wait X seconds before considering the server as down. This way if the user is leaving the page, the timeout will prevent him from viewing the "server is down" message. – Salvatorelab Feb 13 '13 at 12:00
  • 2
    @Mitch Wheat If you are a developer it's definetly not rare :) – Adam Halasz Feb 13 '13 at 12:03

1 Answers1

2

Perhaps this is a bit of a naive approach, but you could count the number of response headers when an ajax call fails. With jQuery, for instance, you could do this:

$.ajax( {
    'url': urlToTheServer,
    'success': function()
    {
        // server is up
    },
    'error': function( $xhr )
    {
        if( $xhr.getAllResponseHeaders().length < 1 )
        {
            // fair probability server is down
        }
    }
} );

The way I tested this, on my development box, is wrap the above in a setTimeout() call (with say, a 10 sec. timeout). Load the page. Shutdown server. And then wait for the timeout to expire and do the ajax request.

PS.: $xhr.getAllResponseHeaders() returns a newline delimited string of headers, not an array or object of headers. I overlooked this at first, however, this doesn't change the outcome of the script. If the string length of $xhr.getAllResponseHeaders() is less than 1, no response headers were present.

Decent Dabbler
  • 21,401
  • 8
  • 70
  • 101
  • Do you mean if the server fails there are no headers? – Adam Halasz Feb 13 '13 at 12:10
  • Yes, I just tested this with shutting down my Apache server. It works the way I expected. – Decent Dabbler Feb 13 '13 at 12:11
  • This works if I send the request while the server is down. And this is what I actually asked in my question. So I accept your answer. However I just realized that what I'm really looking for is checking if a forever keep-alive ajax request fails because of a server shutdown or by user abort(ex: navigating away), but I'm making a new question out of this. – Adam Halasz Feb 14 '13 at 20:55
  • @Adam Ah, I see. My apologies for not interpreting your question to its full extent. Good luck with the other question. To be honest: I'm not even sure I fully understand what a "forever keep-alive ajax request" actually means, so I'll be following your next question with interest. – Decent Dabbler Feb 14 '13 at 21:01
  • 1
    here it is http://stackoverflow.com/questions/14883990/check-request-fail-reason-in-javascript – Adam Halasz Feb 14 '13 at 21:04