patternjavascriptMinor
Is it long polling or short polling?
Viewed 0 times
longpollingshort
Problem
I wrote the following script and php code for long-polling architecture by googling references, I would like to know if this is long-polling or short-polling, ...because I am not sure:
bc_test.js
PHP
bc_test.js
jQuery(function($) {
var data = [];
function pullNotification() {
var params = {};
new RPC.Call({
'method': 'users.getJsonUsers',
'params': params,
'timeout': 30000,
'onSuccess': success,
'onFailure' : error,
});
};
function error() {
alert('Error occured');
}
function success(result) {
if (data.length !== 0) {
for (var i = 0, item; item = result[i]; i++) {
for (var k = 0; k \n\\n\
' + item.userid + '\n\
' + item.alias + '\n\
' + item.surname + '\n\
').insertAfter('.nilar');
}
this.found = false;
}
} else {
for (var j = 0; j < result.length; j++) {
data.push(result[j].userid);
}
}
}
window.setTimeout(function() {
window.setInterval(function() {
pullNotification();
}, 3000);
pullNotification();
}, 1000);
});PHP
function getJsonUsers() {
$timeStart = time();
$newData = false;
$users_data = array();
$sql = 'SELECT * FROM users';
while (!$newData && (time() - $timeStart) < 10) {
usleep ( 10000000 );
$db_res = DBselect($sql);
while ($user = DBfetch($db_res)) {
$users_data[] = $user;
$newData = true;
}
}
return $users_data;
}Solution
This code is performing a long-polling technique. The server gets a client request, the server method is checking for any changes in data, and when found, returns it to the client. In short-polling, the client is constantly sending requests for data on a timer basis; in long-polling, the client makes one request and waits for the server to return any new data it has.
For example, in your method getJsonUsers(), it is running a query that looks for new data, and upon finding it, returns it to the user. If it doesn't find data, it waits a finite amount of time, and tries again. The client won't get notified of anything until data is actually returned, or it will timeout. This is a normal pattern for long-polling. If your program was using a short-polling technique, the server method would just return (either data or no data, depending on the results of the query you're running), and not wait to actually get something to give back to the client.
Your client code on the other hand, in the case of short-polling, would keep making requests to the server, and after a certain amount of time, would make another request. Long-polling uses an event-based mechanism (that is, it waits for data to be present prior to returning) for alerting the client of data, whereas the short-polling method just keeps hitting the server regardless of what it gets back or not.
This website has good information on polling techniques, including an example of long-polling: CoderTalks: Long Polling vs Short Polling
For example, in your method getJsonUsers(), it is running a query that looks for new data, and upon finding it, returns it to the user. If it doesn't find data, it waits a finite amount of time, and tries again. The client won't get notified of anything until data is actually returned, or it will timeout. This is a normal pattern for long-polling. If your program was using a short-polling technique, the server method would just return (either data or no data, depending on the results of the query you're running), and not wait to actually get something to give back to the client.
Your client code on the other hand, in the case of short-polling, would keep making requests to the server, and after a certain amount of time, would make another request. Long-polling uses an event-based mechanism (that is, it waits for data to be present prior to returning) for alerting the client of data, whereas the short-polling method just keeps hitting the server regardless of what it gets back or not.
This website has good information on polling techniques, including an example of long-polling: CoderTalks: Long Polling vs Short Polling
Context
StackExchange Code Review Q#62593, answer score: 5
Revisions (0)
No revisions yet.