You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reactive extensions for PHP. The reactive extensions for PHP are a set of
libraries to compose asynchronous and event-based programs using observable
streams.
note: When running the demos, the scheduler is automatically bootstrapped. When using RxPHP within your own project, you'll need to set the default scheduler.
Installation
Install an event loop. Any event loop should work, but the ReactPHP event loop is recommended.
<?phprequire_once__DIR__ . '/vendor/autoload.php';
useRx\Observable;
useReact\EventLoop\Factory;
useRx\Scheduler;
$loop = Factory::create();
//You only need to set the default scheduler once
Scheduler::setDefaultFactory(function() use($loop){
returnnewScheduler\EventLoopScheduler($loop);
});
Observable::interval(1000)
->take(5)
->flatMap(function ($i) {
return Observable::of($i + 1);
})
->subscribe(function ($e) {
echo$e, PHP_EOL;
});
$loop->run();
Working with Promises
Some async PHP frameworks have yet to fully embrace the awesome power of observables. To help ease the transition, RxPHP has built in support for ReactPHP promises.