- Notifications
You must be signed in to change notification settings - Fork143
Reactive extensions for PHP
License
ReactiveX/RxPHP
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Reactive extensions for PHP. The reactive extensions for PHP are a set oflibraries to compose asynchronous and event-based programs using observablestreams.
$source = \Rx\Observable::fromArray([1,2,3,4]);$source->subscribe(function ($x) {echo'Next:',$x,PHP_EOL; },function (Exception$ex) {echo'Error:',$ex->getMessage(),PHP_EOL; },function () {echo'Completed',PHP_EOL; });//Next: 1//Next: 2//Next: 3//Next: 4//Completed
$ git clone https://github.com/ReactiveX/RxPHP.git$cd RxPHP$ composer install$ php demo/interval/interval.php
Have fun running the demos in/demo
.
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.
- Install an event loop. Any event loop should work, but the ReactPHP event loop is recommended.
$ composer require react/event-loop
- Install RxPHP usingcomposer.
$ composer require reactivex/rxphp
- Write some code.
<?phprequire_once__DIR__ .'/vendor/autoload.php';useRx\Observable;useReact\EventLoop\Factory;useRx\Scheduler;$loop = Factory::create();//You only need to set the default scheduler onceScheduler::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();
Some async PHP frameworks have yet to fully embrace the awesome power of observables. To help ease the transition, RxPHP has built in support forReactPHP promises.
Mixing a promise into an observable stream:
Observable::interval(1000) ->flatMap(function ($i) {return Observable::fromPromise(\React\Promise\resolve(42 +$i)); }) ->subscribe(function ($v) {echo$v .PHP_EOL; });
Converting an Observable into a promise. (This is useful for libraries that use generators and coroutines):
$observable = Observable::interval(1000) ->take(10) ->toArray() ->map('json_encode');$promise =$observable->toPromise();
RxPHP is licensed under the MIT License - see the LICENSE file for details
About
Reactive extensions for PHP
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.