PHP code example of brownpaperbag / flush

1. Go to this page and download the library: Download brownpaperbag/flush library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brownpaperbag / flush example snippets




use BrownPaperBag\Flush;

$flush = new Flush('application/json');

$flush->prepare();

do{

    if(!$flush->signal()){
    
        break;
    
    }
    else{
    
        // Your commands here or conditions to break in some point...
    
    }

}
while(sleep(1) || true);

$flush->json(array(

    'command' => 'location.reload();'

));



use BrownPaperBag\Flush;

$flush = new Flush('application/json');

$flush->prepare();

do{

    $message = rand(1, 10);

    if(!$flush->signal()){

        break;

    }
    else if($message % 2){ // Any condition you want...

        $flush->json(array(

            'success' => true,

            'message' => $message

        ));

    }

}
while(sleep(1) || true);



use BrownPaperBag\Flush;

// You could use new Flush('application/json', true) instead of commands bellow.
// Just showing another way...

$flush = new Flush();

$flush->setContentType('application/json'); // Optional
$flush->setLimboEnabled(true); // Required for zombie requests

// Doing whatever task that user must to wait...

$flush->json(array( // If limbo is enabled Flush will prepare() automatically.

    'message' => 'An awesome return message here...'

)); // To prevent prepare(), for any reason, just pass an explicit false as second argument in json/data().

// From this moment you can execute any task in background like: render a PDF, send emails or fire some slow script.
// Just avoid to create an infinite loop :)