# @socket-mesh/writable-consumable-stream

> An async stream which can be iterated over using a for-await-of loop.

Latest version **4.3.3** (published 2026-04-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @socket-mesh/writable-consumable-stream
pnpm add @socket-mesh/writable-consumable-stream
yarn add @socket-mesh/writable-consumable-stream
bun add @socket-mesh/writable-consumable-stream
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.3.3 |
| Published | 2026-04-11 |
| First published | 2023-03-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 17.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jonathan Gros-Dubois |
| Maintainers | gak10100 |
| Keywords | async, generator, stream, data, for-await-of |

## Links

- npm: https://www.npmjs.com/package/@socket-mesh/writable-consumable-stream
- Repository: https://github.com/socket-mesh/formatter
- Homepage: https://github.com/socket-mesh/writable-consumable-stream#readme
- Issues: https://github.com/socket-mesh/writable-consumable-stream/issues
- npm.io page: https://npm.io/package/@socket-mesh/writable-consumable-stream

## Dependencies (1)

- [@socket-mesh/consumable-stream](https://npm.io/package/@socket-mesh/consumable-stream.md) ^3.3.3

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 4.3.3 (latest) — 2026-04-11
- 4.3.2 — 2026-04-11
- 4.3.1 — 2025-11-12
- 4.2.9 — 2023-10-21
- 4.2.8 — 2023-10-09
- 4.2.7 — 2023-10-09
- 4.2.6 — 2023-10-09
- 4.2.5 — 2023-10-09
- 4.2.4 — 2023-10-09
- 4.2.3 — 2023-10-05
- 4.2.2 — 2023-10-05
- 4.2.1 — 2023-10-04
- 4.2.0 — 2023-04-01
- 4.1.0 — 2023-04-01
- 4.0.0 — 2023-03-31

## README

# writable-consumable-stream
An async stream which can be iterated over using a for-await-of loop and which can be written to.

The `WritableConsumableStream` class extends the `ConsumableStream` class.  
See https://github.com/SocketCluster/consumable-stream

## Installation

```
npm install @socket-mesh/writable-consumable-stream
```

## Usage

### Require

```js
const WritableConsumableStream = require('writable-consumable-stream');
```

### Consume a stream and write to it asynchronously:

```js
let consumableStream = new WritableConsumableStream();

async function consumeAsyncIterable(asyncIterable) {
  // Consume iterable data asynchronously.
  for await (let packet of asyncIterable) {
    console.log('Packet:', packet);
  }
}
consumeAsyncIterable(consumableStream);

setInterval(() => {
  // Write data to the stream asynchronously,
  consumableStream.write(`Timestamp: ${Date.now()}`);
}, 100);
```

### Consume a stream using a while loop:

```js
let consumableStream = new WritableConsumableStream();

async function consumeAsyncIterable(asyncIterable) {
  // Consume iterable data asynchronously.
  // Works in older environments.
  let asyncIterator = asyncIterable.createConsumer();
  while (true) {
    let packet = await asyncIterator.next();
    if (packet.done) break;
    console.log('Packet:', packet.value);
  }
}
consumeAsyncIterable(consumableStream);

setInterval(() => {
  // Write data to the stream asynchronously,
  consumableStream.write(`Timestamp: ${Date.now()}`);
}, 100);
```

### Consume a filtered stream using an async generator:

```js
let consumableStream = new WritableConsumableStream();

// Creates an async generator which only produces packets which are allowed by the
// specified filterFunction.
async function* createFilteredStreamGenerator(fullStream, filterFunction) {
  for await (let packet of fullStream) {
    if (filterFunction(packet)) {
      yield packet;
    }
  }
}

async function consumeAsyncIterable(asyncIterable) {
  // Consume iterable data asynchronously.
  for await (let packet of asyncIterable) {
    console.log('Packet:', packet);
  }
}

// The filter function will only include strings which end with the number 5.
function filterFn(data) {
  return /5$/.test(data);
}
let filteredStreamGenerator = createFilteredStreamGenerator(consumableStream, filterFn);

consumeAsyncIterable(filteredStreamGenerator);

setInterval(() => {
  // Write data to the stream asynchronously,
  consumableStream.write(`Timestamp: ${Date.now()}`);
}, 100);
```

### Consume only the next data object which will be written to the stream:

```js
let consumableStream = new WritableConsumableStream();

(async () => {
  let data = await consumableStream.once();
  console.log(data);
})();

setInterval(() => {
  // Write data to the stream asynchronously,
  consumableStream.write(`Timestamp: ${Date.now()}`);
}, 100);
```

See `test/` directory for additional examples.

---
_Source: https://npm.io/package/@socket-mesh/writable-consumable-stream · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
