Skip to content

Instantly share code, notes, and snippets.

@clouddueling
Created March 26, 2013 02:07
Show Gist options
  • Save clouddueling/5242554 to your computer and use it in GitHub Desktop.
Save clouddueling/5242554 to your computer and use it in GitHub Desktop.
Long pulling in php with Laravel
public function get_checkin_poll()
{
$filepath = 'storage/work/checkin_poll_' . Auth::user()->id;
$last_cycle = File::get($filepath);
$break = false;
$thread = uniqid();
// if last cycle was > 60 secs continue
$time_dif = time() - $last_cycle;
Log::last($time_dif);
if ($time_dif > 60) {
File::put($filepath, time());
exit;
}
// create an array to hold the types of polling actions
$json = array(
'contact' => null,
'service_queue_count' => null
);
while (1) {
Log::cycle($thread);
// get the updated value of activity_at for logged in user.
$user = User::find(Auth::user()->id);
// if last actiiity was > 40 seconds stop
$active_dif = time() - strtotime($user->activity_at);
Log::active($active_dif);
if ($active_dif > 60)
$break = true;
// Check for requesting patients
// If there are any contacts requesting to be verified show them.
$contact = Contact::where_account_user_id(Auth::user()->account_user_id)
->where_request(1)
->first();
if ($contact != null) {
$json['contact'] = $contact->to_array();
$break = true;
}
// Check the service queue
// if the number of contacts != this count of contacts reload the services queue
$pivot_count = DB::table('contact_dashboard')
->where_account_user_id(Auth::user()->account_user_id)
->count();
Log::queue($pivot_count);
// get the value to be compared in future.
if ($json['service_queue_count'] == null) {
$json['service_queue_count'] = $pivot_count;
}
if ($json['service_queue_count'] != $pivot_count) {
$break = true;
}
if ($break)
break;
// save time of last cycle
File::put($filepath, time());
sleep(3);
}
File::put($filepath, '');
return Response::json($json);
}
function waitForMsg() {
$.get('/focus/contact/checkin_poll', function(data){
if (data != '') {
if (data.contact != undefined) {
var view = viewVerify(data.contact);
s.contact = data.contact;
$('.verify-contact').show(0).html(view);
}
Service.loadServices();
}
setTimeout(function(){
waitForMsg()
}, 3000);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment