I have a connector that will call the RESP API using cURL and PHP.
I need to call one method each time to check for new messages and then process them. I used the following two approaches for processing messages.
- AJAX polling using
SetInterval(): call the PHP script once per second. This works fine, except that I cannot prevent multiple SetInterval()browser tabs from starting simultaneously . (I do not want the user to open 10 browser tabs, which leads to the fact that one user having 10 SetInterval()works simultaneously. - Events sent by the server using
EventSource: the server will send a browser update after new data appears in the queue. This slows down the response time. Each call I make for a script takes about 20 seconds to complete the problem. I am not sure why this is happening.
Here is my implementation SetInterval()
function startCalls(){
refreshIntervalId = setInterval(function() {
$.getJSON("index.php", {'method': 'getMessages', 'jSON': true} , function(data){
processServerData(data);
});
}, 1000);
}
As soon as the user logs in, I call this function startCalls()
inside the index.php file I have this code to be called
if($method == 'getMessages'){
$messaging = new ICWS\Messaging($icws);
$messaging->processMessages();
$myQueue = $messaging->getCallsQueue();
echo json_encode($myQueue );
}
Here is my second implementation of "Server Sent Events"
function startPolling(evtSource){
evtSource.addEventListener("getMessagingQueue", function(e) {
var data = JSON.parse(e.data);
processServerData(data)
}, false);
}
As soon as the user logs in, I call this function startPolling( new EventSource("poll.php") );
For simplicity, suppose my method is processServerDataas follows
function processServerData(data){
console.log('process the data received from the server');
}
here is my php code
<?php
require 'autoloader.php';
if(!is_callable('curl_init')){
exit('cURL is disabled on this server. Before making API calls cURL extension must be enabled.');
}
if( isset($_COOKIE['icws_username']) ){
$user = trim($_COOKIE['icws_username']);
}
if( isset($_COOKIE['icws_password']) ){
$pass = trim($_COOKIE['icws_password']);
}
if( isset($_COOKIE['icws_station']) ){
$stationName = trim($_COOKIE['icws_station']);
}
if( !isset($user, $pass, $stationName) ){
echo json_encode(array('status' => 'The IC credentials you entered are invalid'));
exit;
}
$scheme = 'http';
$host = 'host';
$port = '8018';
$sleepTime = 1;
$url = sprintf('%s://%s:%s@%s:%s', $scheme, $user, $pass, $host, $port);
try {
header("Content-Type: text/event-stream\n\n");
session_start();
$conf = new ICWS\Config\Config($url, $stationName);
$attrebutes = array();
$icws = new ICWS\Connection($conf, $attrebutes, true);
$messaging = new ICWS\Messaging($icws);
ob_end_clean();
while(true){
header("Content-Type: text/event-stream" . PHP_EOL);
header("Cache-Control: no-cache" . PHP_EOL);
$messaging->processMessages();
$result = $messaging->getCallsQueue();
echo 'event: getMessagingQueue' . PHP_EOL;
echo 'data: ' . json_encode( $result) . PHP_EOL;
echo PHP_EOL;
ob_end_flush();
flush();
sleep(1);
}
} catch(Exception $e){
echo $e->getMessage();
}
?>
, , , , " ", ,
Server-Sent ?