2017 © Pedro Peláez
 

library flush

Library to flush data friendly. To use in long-polling and zombie requests.

image

brownpaperbag/flush

Library to flush data friendly. To use in long-polling and zombie requests.

  • Tuesday, March 29, 2016
  • by eagostini
  • Repository
  • 6 Watchers
  • 1 Stars
  • 70 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Presentation

Library to flush data friendly. To use in long-polling and zombie requests. Distributed as a composer package., (*1)

Instalation

composer require brownpaperbag/flush

Usage

This library was built to work in three scenarios basically:, (*2)

  • Long-polling requests;
  • Long polling requests with socket style;
  • Zombie requests;

Usage in long-polling requests

<?php

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();'

));

Usage in long-polling requests with socket style

<?php

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);

That's an example how to receive the response on client side:, (*3)

var request = new XMLHttpRequest();

request.onreadystatechange = function(){

    if(this.status == 200){

        switch(this.readyState){

            case this.LOADING:

                var data = this.responseText,
                    length = data.length;

                data = data.slice(this.lastLength - length);
                data = data.trim(data);

                this.lastLength = length;

                if(data.length){

                    data = $.parseJSON(data);

                    // Whatever you want to do with your new object.
                    // You can use it to implement push notifications for example...

                    console.log(data);

                }

                break;

        }

    }

};

request.open('GET', '/api/notifications', true);
request.send();

Usage in zombie requests

<?php

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 :)

License

MIT, (*4)

The Versions

29/03 2016

dev-master

9999999-dev

Library to flush data friendly. To use in long-polling and zombie requests.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Eduardo Agostini

requests flush chunked zoobie long-polling

30/10 2014

1.0.0

1.0.0.0

Library to flush data friendly. To use in long-polling and zombie requests.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Eduardo Agostini

requests flush chunked zoobie long-polling