Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ReactiveX/RxPHP
Reactive extensions for PHP
https://github.com/ReactiveX/RxPHP
asynchronous observables reactivex rxphp
Last synced: about 2 months ago
JSON representation
Reactive extensions for PHP
- Host: GitHub
- URL: https://github.com/ReactiveX/RxPHP
- Owner: ReactiveX
- License: mit
- Created: 2016-01-23T20:44:18.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2024-09-23T19:18:48.000Z (3 months ago)
- Last Synced: 2024-10-16T05:41:37.471Z (about 2 months ago)
- Topics: asynchronous, observables, reactivex, rxphp
- Language: PHP
- Size: 992 KB
- Stars: 1,697
- Watchers: 94
- Forks: 140
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-php - RxPHP - A reactive extension library. (Table of Contents / Event)
- awesome-php-cn - RxPHP - 反应性扩展库. (目录 / 事件 Event)
- awesome-projects - RxPHP - A reactive extension library. (PHP / Event)
- awesome-php - RxPHP - A reactive extension library. (Table of Contents / Event)
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
streams.[![CI status](https://github.com/ReactiveX/RxPHP/workflows/CI/badge.svg)](https://github.com/ReactiveX/RxPHP/actions)
[![Coverage Status](https://coveralls.io/repos/github/ReactiveX/RxPHP/badge.svg?branch=master)](https://coveralls.io/github/ReactiveX/RxPHP?branch=master)## Example
```php
$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```
## Try out the demos
```bash
$ 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.
## Installation
1. Install an event loop. Any event loop should work, but the ReactPHP event loop is recommended.```bash
$ composer require react/event-loop
```2. Install RxPHP using [composer](https://getcomposer.org).
```bash
$ composer require reactivex/rxphp
```3. Write some code.
```PHP
take(5)
->flatMap(function ($i) {
return Observable::of($i + 1);
})
->subscribe(function ($e) {
echo $e, PHP_EOL;
});$loop->run();
```
## Working with PromisesSome 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](https://github.com/reactphp/promise).
Mixing a promise into an observable stream:
```PHP
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):
```PHP
$observable = Observable::interval(1000)
->take(10)
->toArray()
->map('json_encode');$promise = $observable->toPromise();
```## Additional Information
- [The Official Reactive Extensions Documentation](http://reactivex.io/documentation/observable.html)
- [A Simple Introduction to Observables](https://www.youtube.com/watch?v=uQ1zhJHclvs)(video)## License
RxPHP is licensed under the MIT License - see the LICENSE file for details