I’m coding an auction website in Laravel 5.0 that simulates realtime updates by using an AJAX poller that is executed every 5 seconds. The problem is that my server returns sporadic HTTP 401 status.
My route is build like this:
Route::post(auction/live/update, 'AuctionController@ajaxSendUpdate');
My controller is like this:
public function ajaxSendUpdate() {
// Business logic: queries database, couple of Ifs, etc…
$data = array('success' => true, 'otherStuff' => $myData);
return Response::json($data);
}
Finally my poller is setup like this:
// a bit of HTML
function getAuctionUpdate() {
setTimeout(function () {
$.ajax({
type: "POST",
url: "{!! url('auction/live/update')!!}",
dataType: 'json',
data: {
auctionID: $('#auctionID').val()
},
success: function (data) {
if (data['success']) {
// Updates some labels, etc.
getAuctionUpdate(); // Rearms itself
}
}
} }); // Not sure if all brackets are correct in this snippet but they are 100% on real code
}, 5000);
This code runs fine about 95% of times. However it can break with 2 different outcomes:
1) Server responds error 401 after some time and never recovers. In this scenario we need to login again. After login, everything goes well and this outcome never occurs again.
2) Server responds with sporadic 401 but recovers in the next (or after a few) polling requests.
I’m using Laravel 5.0 and an up-to-date version of Xampp on Windows. The error is easily reproduced with WAMP on Windows. Not tested in Linux nor OSX. I've read this and this and assorted threads in laracasts.com and other forums but I am unable to solve the problem...