ReactiveX/RxPHP

Name: RxPHP

Owner: ReactiveX

Description: Reactive extensions for PHP

Created: 2016-01-23 20:44:18.0

Updated: 2018-05-23 14:54:32.0

Pushed: 2018-05-01 10:09:55.0

Homepage: null

Size: 958

Language: PHP

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

RxPHP

Reactive extensions for PHP. The reactive extensions for PHP are a set of libraries to compose asynchronous and event-based programs using observable collections and LINQ-style query operators in PHP.

Build Status Coverage Status

note: This repo is for v2.x, the latest version of RxPHP, not v1.x.

Example
rce = \Rx\Observable::fromArray([1, 2, 3, 4]);

rce->subscribe(
function ($x) {
    echo 'Next: ', $x, PHP_EOL;
},
function (Exception $ex) {
    echo 'Error: ', $ex->getMessage(), PHP_EOL;
},
function () {
    echo 'Completed', PHP_EOL;
}


xt: 1
xt: 2
xt: 3
xt: 4
mpleted
Try out the demos
t clone https://github.com/ReactiveX/RxPHP.git
 RxPHP
mposer install
p 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.

Installation
  1. Install an event loop. Any event loop should work, but the ReactPHP event loop is recommended.
mposer require react/event-loop
  1. Install RxPHP using composer.
mposer require reactivex/rxphp
  1. Write some code.
p

ire_once __DIR__ . '/vendor/autoload.php';

Rx\Observable;
React\EventLoop\Factory;
Rx\Scheduler;

p = Factory::create();

u only need to set the default scheduler once
duler::setDefaultFactory(function() use($loop){
return new Scheduler\EventLoopScheduler($loop);


rvable::interval(1000)
->take(5)
->flatMap(function ($i) {
    return Observable::of($i + 1);
})
->subscribe(function ($e) {
    echo $e, PHP_EOL;
});

p->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.

Mixing a promise into an observable stream:

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

ervable = Observable::interval(1000)
->take(10)
->toArray()
->map('json_encode');

mise = $observable->toPromise();
Additional Information
License

RxPHP is licensed under the MIT License - see the LICENSE file for details


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.