Thanks in advance I have used periodic Ajax call for getting a value from the API result in WordPress with Ajax Here is my code
Javascript code is :
setInterval('getRecentAPI(oid)', 60000);
function getRecentAPI(oid){
jQuery.ajax({
type: "POST",
url: ajaxurl,
data: {id:oid, action:'recent_value'},
success: function(result){
jQuery(".recent div").html(result);
}
});
}
WordPress code is (php code) is:
function display_recent_value(){
$id = trim($_POST['id']);
$api_call= api_call($id);
$req_result = json_decode($api_call);
echo $api_values = $req_result->data->value;
die();
}
add_action( 'wp_ajax_recent_value', 'display_recent_value' );
add_action( 'wp_ajax_nopriv_recent_value', 'display_recent_value');
API call function (php function) is :
function api_call($id){
$URL = 'https://www.example.net/api/result?id='.$id;
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
$result = curl_exec($ch);
return $result;
}
This works fine, but my client wants to implement this feature using "Web sockets" or "server push" instead of Periodic Ajax call. How can i proceed with "web sockets" or "server push"? Thoughts would be muchly appreciated.