PHPackages                             nick4fake/promise-stream-react - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. nick4fake/promise-stream-react

Abandoned → [nick4fake/promise-stream-react](/?search=nick4fake%2Fpromise-stream-react)Library[Queues &amp; Workers](/categories/queues)

nick4fake/promise-stream-react
==============================

The missing link between Promise-land and Stream-land for React PHP

v0.1.2(9y ago)010MITPHPPHP &gt;=5.3

Since Apr 12Pushed 8y ago1 watchersCompare

[ Source](https://github.com/nick4fake/php-promise-stream-react)[ Packagist](https://packagist.org/packages/nick4fake/promise-stream-react)[ Docs](https://github.com/clue/php-promise-stream-react)[ RSS](/packages/nick4fake-promise-stream-react/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (5)Versions (4)Used By (0)

clue/promise-stream-react [![Build Status](https://camo.githubusercontent.com/bee75071610437009070792bd72b63f9ccffed5e2a744fe7c874a629e0ec1882/68747470733a2f2f7472617669732d63692e6f72672f636c75652f7068702d70726f6d6973652d73747265616d2d72656163742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/clue/php-promise-stream-react)
================================================================================================================================================================================================================================================================================================================================================

[](#cluepromise-stream-react-)

The missing link between Promise-land and Stream-land, built on top of [React PHP](http://reactphp.org/).

**Table of Contents**

- [Usage](#usage)
    - [buffer()](#buffer)
    - [first()](#first)
    - [all()](#all)
    - [unwrapReadable()](#unwrapreadable)
    - [unwrapWritable()](#unwrapwritable)
- [Install](#install)
- [License](#license)

> Note: This project is in beta stage! Feel free to report any issues you encounter.

Usage
-----

[](#usage)

This lightweight library consists only of a few simple functions. All functions reside under the `Clue\React\Promise\Stream` namespace.

The below examples assume you use an import statement similar to this:

```
use Clue\React\Promise\Stream;

Stream\buffer(…);
```

Alternatively, you can also refer to them with their fully-qualified name:

```
\Clue\React\Promise\Stream\buffer(…);
```

### buffer()

[](#buffer)

The `buffer(ReadableStreamInterface $stream)` function can be used to create a `Promise` which resolves with the stream data buffer.

```
$stream = accessSomeJsonStream();

Stream\buffer($stream)->then(function ($contents) {
    var_dump(json_decode($contents));
});
```

The promise will resolve with all data chunks concatenated once the stream closes.

The promise will resolve with an empty string if the stream is already closed.

The promise will reject if the stream emits an error.

The promise will reject if it is canceled.

### first()

[](#first)

The `first(ReadableStreamInterface|WritableStreamInterface $stream, $event = 'data')`function can be used to create a `Promise` which resolves once the given event triggers for the first time.

```
$stream = accessSomeJsonStream();

Stream\first($stream)->then(function ($chunk) {
    echo 'The first chunk arrived: ' . $chunk;
});
```

The promise will resolve with whatever the first event emitted or `null` if the event does not pass any data. If you do not pass a custom event name, then it will wait for the first "data" event and resolve with a string containing the first data chunk.

The promise will reject once the stream closes – unless you're waiting for the "close" event, in which case it will resolve.

The promise will reject if the stream is already closed.

The promise will reject if it is canceled.

### all()

[](#all)

The `all(ReadableStreamInterface|WritableStreamInterface $stream, $event = 'data')`function can be used to create a `Promise` which resolves with an array of all the event data.

```
$stream = accessSomeJsonStream();

Stream\all($stream)->then(function ($chunks) {
    echo 'The stream consists of ' . count($chunks) . ' chunk(s)';
});
```

The promise will resolve with an array of whatever all events emitted or `null` if the events do not pass any data. If you do not pass a custom event name, then it will wait for all the "data" events and resolve with an array containing all the data chunks.

The promise will resolve with an array once the stream closes.

The promise will resolve with an empty array if the stream is already closed.

The promise will reject if the stream emits an error.

The promise will reject if it is canceled.

### unwrapReadable()

[](#unwrapreadable)

The `unwrapReadable(PromiseInterface $promise)` function can be used to unwrap a `Promise` which resolves with a `ReadableStreamInterface`.

This function returns a readable stream instance (implementing `ReadableStreamInterface`) right away which acts as a proxy for the future promise resolution. Once the given Promise resolves with a `ReadableStreamInterface`, its data will be piped to the output stream.

```
//$promise = someFunctionWhichResolvesWithAStream();
$promise = startDownloadStream($uri);

$stream = Stream\unwrapReadable($promise);

$stream->on('data', function ($data) {
   echo $data;
});

$stream->on('end', function () {
   echo 'DONE';
});
```

If the given promise is either rejected or fulfilled with anything but an instance of `ReadableStreamInterface`, then the output stream will emit an `error` event and close:

```
$promise = startDownloadStream($invalidUri);

$stream = Stream\unwrapReadable($promise);

$stream->on('error', function (Exception $error) {
    echo 'Error: ' . $error->getMessage();
});
```

The given `$promise` SHOULD be pending, i.e. it SHOULD NOT be fulfilled or rejected at the time of invoking this function. If the given promise is already settled and does not resolve with an instance of `ReadableStreamInterface`, then you will not be able to receive the `error` event.

### unwrapWritable()

[](#unwrapwritable)

The `unwrapWritable(PromiseInterface $promise)` function can be used to unwrap a `Promise` which resolves with a `WritableStreamInterface`.

This function returns a writable stream instance (implementing `WritableStreamInterface`) right away which acts as a proxy for the future promise resolution. Once the given Promise resolves with a `WritableStreamInterface`, any data you wrote to the proxy will be piped to the inner stream.

```
//$promise = someFunctionWhichResolvesWithAStream();
$promise = startUploadStream($uri);

$stream = Stream\unwrapWritable($promise);

$stream->write('hello');
$stream->end('world');

$stream->on('close', function () {
   echo 'DONE';
});
```

If the given promise is either rejected or fulfilled with anything but an instance of `WritableStreamInterface`, then the output stream will emit an `error` event and close:

```
$promise = startUploadStream($invalidUri);

$stream = Stream\unwrapWritable($promise);

$stream->on('error', function (Exception $error) {
    echo 'Error: ' . $error->getMessage();
});
```

The given `$promise` SHOULD be pending, i.e. it SHOULD NOT be fulfilled or rejected at the time of invoking this function. If the given promise is already settled and does not resolve with an instance of `WritableStreamInterface`, then you will not be able to receive the `error` event.

Install
-------

[](#install)

The recommended way to install this library is [through Composer](https://getcomposer.org). [New to Composer?](https://getcomposer.org/doc/00-intro.md)

This will install the latest supported version:

```
$ composer require clue/promise-stream-react:^0.1.2
```

See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.

License
-------

[](#license)

MIT

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~22 days

Total

3

Last Release

3640d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/533d0c99c4e0c7413a1dcce2d81c37ecf94883e6377d274d6f43247a399bb268?d=identicon)[nick4fake](/maintainers/nick4fake)

---

Top Contributors

[![clue](https://avatars.githubusercontent.com/u/776829?v=4)](https://github.com/clue "clue (21 commits)")

---

Tags

streamasyncpromisereactphpBufferunwrap

### Embed Badge

![Health badge](/badges/nick4fake-promise-stream-react/health.svg)

```
[![Health](https://phpackages.com/badges/nick4fake-promise-stream-react/health.svg)](https://phpackages.com/packages/nick4fake-promise-stream-react)
```

###  Alternatives

[react/promise-stream

The missing link between Promise-land and Stream-land for ReactPHP

11512.9M45](/packages/react-promise-stream)[react/socket

Async, streaming plaintext TCP/IP and secure TLS socket server and client connections for ReactPHP

1.3k116.9M402](/packages/react-socket)[react/promise-timer

A trivial implementation of timeouts for Promises, built on top of ReactPHP.

34141.9M96](/packages/react-promise-timer)[icicleio/icicle

Icicle is a PHP library for writing asynchronous code using synchronous coding techniques.

1.1k150.9k14](/packages/icicleio-icicle)[clue/docker-react

Async, event-driven access to the Docker Engine API, built on top of ReactPHP.

113154.9k1](/packages/clue-docker-react)[clue/reactphp-flux

Flux, the lightweight stream processor to concurrently do many (but not too many) things at once, built on top of ReactPHP.

59118.6k1](/packages/clue-reactphp-flux)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
