Search code examples
phpajaxsessionlaravellong-polling

Laravel : Update session while Long Polling


My framework is PHP Laravel 4. I'm using long polling technique to refresh a list on my HTML page. In the same page I have an auto submitting dropdown list. When user selects an option it just auto submits to reset a session value. After resetting the session page will refresh. But there is a bug here. When ever I start the polling session is not updating properly. Sometimes it updates but most of the times it won't.

This is what I'm trying to do in the AJAX long polling PHP server side function

public function getNewAppt() {

    //Session is open now. Connection to the server is opened. Session file is locked
    //Close the session
    if (session_status() != PHP_SESSION_NONE) {
        session_write_close();//close
    }

    $today_appt_count = 0;
    $result = Appointment::with(array('user' => function($query) {
                    $query->where('branch_id', Session::get('branch_id'));
                }))->where('start', 'LIKE', date('Y-m-d') . ' %')->get();

    foreach ($result as $appt) {
        if ($appt->user != null) {
            $today_appt_count++;
        }
    }
    return $today_appt_count;
}

When a user changes the dropdown, first I stop the long polling function like this,

console.log('Long Polling has Stopped');
ajaxLongPoll.abort();//Stop the long polling function

and then I'm auto-submitting a html form to this php function.

public function postChangeBranch() {

    //Session is closed now, so start the session
    if (session_status() == PHP_SESSION_NONE) {
        session_start(); //start session again
    }

    Session::forget('branch_id');
    Session::put('branch_id', Input::get('branch_id'));
    return Redirect::to('calendar');
}

This is the long polling ajax function

var poll = function() {
    ajaxLongPoll = $.ajax({
        type: 'GET',
        url: 'calendar/new-appt',
        dataType: 'html',
        success: function(data) {
            //update stuff
            requestCount++;
            console.log(requestCount);//number of times this function runs...
            console.log(data);//this will just print a live count from DB
        },
        error: function(data) {
            console.log(data);
        },
        complete: function() {
            poll();// recall this function after 30 sec
        },
        async: true, /* If set to non-async, browser shows page as "Loading.."*/
        cache: false,
        timeout: 30000 /* Timeout in ms */
    });
}//polling end

Could you please tell me what am I doing wrong here? Thanks a lot!


Solution

  • Some people suggested to close the ajax request before submitting the form by using ajaxRequest.abort(); Check below. Abort Ajax requests using jQuery But it didn't work for me.

    This is what I did,

    I have created a new variable called startPoll and modified my polling ajax function's complete section like below

        ....
        complete: function() {
            if(startPoll)//run only if this is true
            poll();// recall this function after 30 sec
        },
        .....
    

    and then just before I auto submit my from (before refreshing the page), I will set the startPoll value to false so that will stop my polling function.

       //Long polling stop - On change the branch refresh the page
       $('select[name="branch_id"]').on('change', function(e) {
           e.preventDefault();
           console.log('Long Polling has Stopped');
           startPoll = false;
           $('#auto-sub').submit();
       });
    

    Finally this did the trick for me. Hope this will help someone. :)