reactphp/socket

Class method call in background

sharif779 opened this issue · 1 comments

I am using following function for live stream

public function create_live_stream(){
            $loop = React\EventLoop\Factory::create();
            if(file_exists("/tmp/match.sock")){
                unlink("/tmp/match.sock");
            }
            $socket=new React\Socket\UnixServer('/tmp/match.sock', $loop);
            $socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
                $connection->on('data', function ($data) use ($connection) {
                    $arr_data=json_decode($data,true);
                    if(isset($arr_data['match_id'])){
                        $match=$arr_data['match_id'];
                        $url="http://live.wh.sportingpulseinternational.com/v2/basketball/readlog/$match?ak=ak&playbyplayOnConnect=1";
                        $r   = new Reader($match);
                        $r->run($url, 'processdata');
                    }
                });
            });

            $loop->run();
        }

Here

$r   = new Reader($match);
$r->run($url, 'processdata');

is long running process.
I send match id in that function from a loop.Multiple match_id can come ,but only work for one match id.
I want this reader execute in background.How can I do this by reactphp
stream.tar.gz

clue commented

Hi @sharif779, I'm not sure I understand the exact problem you're seeing, so let me try like this:

It looks like your Reader class is using a blocking implementation of an HTTP client. You should replace this with a non-blocking implementation, such as https://github.com/clue/reactphp-buzz:

// turn on streaming responses (does no longer buffer response body)
$browser->withOptions(array('streaming' => true))->get($url)->then(function (ResponseInterface $response) {
    $body = $response->getBody();
    /* @var $body \React\Stream\ReadableStreamInterface */
    
    $body->on('data', function ($chunk) {
        echo $chunk;
    });
});

I hope this helps 👍