Update live data (like progress bar) in view with Laravel 4
ajax, laravel, laravel-4, php
Solution
With PHP there are really two options without going to Web Sockets or Push-Pull setups. This isn't really a Laravel thing it's more of an AJAX loop that requests JSON "thing".
Short polling
Olark uses this methodology for their chat script.
setInterval(function() {
$.getJSON("/path", function(data) {
// update the view with your fresh data
});
}, 5000);
Long polling
Javascript
var eventName = function() {
$.getJSON("/path", function(data) {
// update the view with your fresh data
if (data.progress < 100)
eventName();
});
};
Controller Logic
I use this when I have users upload a CSV and are waiting for it to finish uploading and be processed.
// in your controller
$check = true;
while ($check) {
// search database
// compare values
if ($newDataWasFound)
$check = false;
$progressFromAbove = 90;
}
return Response::json(array(
'newData' => $array,
'progress' => $progressFromAbove,
));
I made a screencast on this using Laravel 3 but Long Polling is PHP relevant not Laravel. https://www.youtube.com/watch?v=LDgJF77jELo
Examples
- https://gist.github.com/clouddueling/5239153
- https://gist.github.com/clouddueling/6296036
Problem
TL;DR I would like to send data to update live in a view, such as a progress bar showing the status of an action. What is the best way to do that in laravel 4? The Setup I'm working on a Laravel 4 based project where each user can redeem a serial key. I've made an admin backend where I can easily paste in a list of keys, or upload a file of them. Let's say `$key_string` is the string of newline-seperated keys that I've uploaded, and want to parse out to then upload the contained key strings from - here is the simplified code that adds the keys: ``` $key_string = rtrim($key_string); $key_string = str_replace("\n\r", "\n", $key_string); $keys = explode( "\n", $key_string); foreach($keys as $index => $key) { Key::create( array( "serial" => trim($key) ) ); } ``` Since the sets of keys I upload number in the thousands, this can sometimes take a good 30 seconds, during which time the admin panel naturally doesn't show anything. Now, I don't mind it taking this time. I don't need to optimize the upload to use one query, etc, but I would like to have some actual feedback so I know how far the upload has gone. The Question When I upload keys, I would like to be able to update a progress bar or counter in my view every few seconds or percent ticks (using the current `$index`) Is there an easy way to handle this painlessly, preferably integrated in Laravel 4? I'm assuming this would involve ajax, but can someone point me in the right direction?