Saturday, September 6, 2014

IP Switching with AJAX-JSON

A web service may be published on more than one IP. In the diagram below a client can access a web service using hypothetical IP addresses on URL addresses:
  • http://aaa.aa.aa.10/web_service
  • http://zzz.zz.zz.15/web_service

This set up is typical for web services handling large volume request bandwidth. If one IP gets overloaded and becomes temporarily unavailable, a switch to the second IP is made.

A client may connect to the web service using an available IP. A routine PING check is made to see if the IP is live. When the IP becomes unavailable, adjustments are made to the web application to connect on the second IP.

This becomes a tedious approach since the check is made manually. The set of codes in this post presents one way to automate IP switching.

A test page on the web service will return a simple JSON response. The idea is for the client  to request the test page using IP#1. If after a defined period and no response was made, IP#1 is assumed to be down and switch to IP#2 is made.

For the test page, json_test.php, a simple JSON is returned as a response:
<?php
//json_test.php
//
header('Content-type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *'); 
$function_name = '';
$source = '';
if (isset($_GET['jsonp'])){
 $function_name = $_GET['jsonp'];
}
$arr['id'] = 'test';
$arr['desc'] = 'ok to proceed';

$json = json_encode($arr);
echo "$function_name (\n";
echo $json;
echo ");\n";
?>


The web client making the JSON IP test is as follows
<!doctype html>
<html>
  <head>
    <title>IP Switch with JSON</title>
 <script src="jquery-1.11.1.min.js"></script>
 <script>
 
 $(document).ready(function (){
  function test_ip(Ip_arr, finalUrl){
   var param = 'json_test.php?action=test&jsonp=?';
   $.ajax({
    url: Ip_arr[0] + param,
    dataType: 'json',
    timeout: 1000,
    success: function(jdata){
     finalUrl('IP1', Ip_arr[0]);
    },
    error: function(xhr, ajaxOptions, thrownError) {
     finalUrl('IP2', Ip_arr[1]);
    }
   });
  }
   
  function start_app(){
   var Ip_arr = ['http://aaa.aa.aa.10/', 'http://zzz.zz.zz.15/'];
   test_ip(Ip_arr, function(Ip_mode, ip){
    alert('IP mode= '+Ip_mode+ '/ IP: '+ip);
    //continue with code
   });
  }
  
  start_app();
 })
 
 </script>
  </head>
  <body>
  </body>
</html>

The start_app() function fires up the web application. This creates an array of IP addresses to  test. This is followed by a call to test_ip() function with the array anonymous function as parameters.












The test_ip() function parameters takes the array of IP addresses to test and a reference to the anonymous function, finalUrl. The function starts with initializing a variable param to pass parameters on the URL. Take not of the jsonp parameter since the web client may make a cross domain call. This is followed by JQuery $.ajax() call.
















The URL setting has the first IP in array and the variable param values, The timeout is set to 1 second- if after this period. If after this period, a JSON response was successful, the first IP is assumed to be live and passes this to the finalUrl anonymous function reference.

Otherwise, there was no JSON response on the timeout. The error handler calls the finalUrl anonymous function reference with the second IP.

The anonymous function is the heart of the web client application. The live IP is now used as needed:























Need help on some web coding task? Let me know about it. Email me at jrwxpun.dolor@gmqoapmssail.co57agsblm and I'll be more than happy to blog about it with my thoughts.

No comments:

Post a Comment