php - Long polling - Message system -


i'm looking doing long polling jquery , php message system. i'm curious know best/most efficient way achieve this. i'm basing off simple long polling example.

if user sitting on inbox page, want pull in new messages. 1 idea i've seen adding last_checked column message table. php script this:

query check null `last_checked` messages if there any... while(...) {     add data array     update `last_checked` column current time } send data 

i idea i'm wondering others think of it. ideal way approach this? information helpful!

to add, there no set number of uses on site i'm looking efficient way it.

yes way describe how long polling method working generally. sample code little vague, add should sleep() small amount of time inside while loop , each time compare last_checked time (which stored on server side) , current time (which sent client's side).

something this:

$current = isset($_get['timestamp']) ? $_get['timestamp'] : 0; $last_checked = getlastcheckedtime(); //returns last time db accessed  while( $last_checked <= $current) {     usleep(100000);     $last_checked = getlastcheckedtime(); }  $response = array(); $response['latestdata'] = getlatestdata() //fetches data want based on time $response['timestamp'] = $last_checked; echo json_encode($response);     

and @ client's side js have this:

function longpolling(){         $.ajax({           type : 'get',           url  : 'data.php?timestamp=' + timestamp,           async : true,           cache : false,            success : function(data) {                 var jsondata = eval('(' + data + ')');                 //do data, eg display them                 timestamp  = jsondata['timestamp'];                 settimeout('longpolling()', 1000);           },           error : function(xmlhttprequest, textstatus, error) {                      alert(error);                     settimeout('longpolling()', 15000);           }             }); } 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -