PHPackages                             lunfel/redis-stream-wrapper - 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. [Caching](/categories/caching)
4. /
5. lunfel/redis-stream-wrapper

ActiveLibrary[Caching](/categories/caching)

lunfel/redis-stream-wrapper
===========================

lunfel/redis-stream-wrapper

1.1.0(1y ago)04PHPPHP ^8.1

Since Feb 5Pushed 1y ago1 watchersCompare

[ Source](https://github.com/lunfel/redis-stream-wrapper)[ Packagist](https://packagist.org/packages/lunfel/redis-stream-wrapper)[ RSS](/packages/lunfel-redis-stream-wrapper/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (2)Dependencies (1)Versions (4)Used By (0)

Redis Stream Wrapper for PHP
============================

[](#redis-stream-wrapper-for-php)

Overview
--------

[](#overview)

This package provides a PHP stream wrapper for Redis, allowing seamless integration of Redis streams with PHP's native file handling functions (`fopen`, `fwrite`, `fread`, etc.).

Features
--------

[](#features)

- Read and write to Redis streams using PHP's built-in stream functions.
- Support for `fopen`, `fread`, `fwrite`, `fclose`, and other standard PHP file operations.
- Handles Redis streams (`XADD`, `XREAD`, etc.) in a transparent way.
- Supports blocking and non-blocking reads.

Requirements
------------

[](#requirements)

- PHP 8.1+
- Redis 5+ (Only tested Redis 7)
- PHP Redis extension (`ext-redis`)

Installation
------------

[](#installation)

You can install the package using Composer:

```
composer lunfel/redis-stream-wrapper
```

Usage
-----

[](#usage)

### Register the Stream Wrapper

[](#register-the-stream-wrapper)

Before using the stream wrapper, you need to register it:

```
stream_wrapper_register("redis", \Lunfel\RedisStreamWrapper\RedisStreamWrapper::class);
```

### Configuring the connection

[](#configuring-the-connection)

#### Configuration reference

[](#configuration-reference)

```
$options = [
    'redis' => [
        // If not provided, defaults to `new Redis()`
        // Supports only phpredis redis client
        'client' => function (): Redis {
            static $client = new Redis([
                'host' => 'redis',
                'port' => 6379,
                'connectTimeout' => 2.5,
                // 'auth' => ['phpredis', 'phpredis'],
                'backoff' => [
                    'algorithm' => Redis::BACKOFF_ALGORITHM_DECORRELATED_JITTER,
                    'base' => 500,
                    'cap' => 750,
                ],
            ]);

            return $client;
        },
        'configurations' => [
            // Prefix added to the Redis stream key.
            // Example: If the prefix is "mystreams:", the path "redis://my/important/file.log"
            // becomes the Redis key "mystreams:my/important/file.log".
            // Default: empty string ('').
            'key_prefix' => 'mystreams:',
            // Specifies the type of internal buffer used by the wrapper to manage data locally.
            // Default: 'php://temp'.
            'internal_stream_uri' => 'php://temp',
            // If set, the Redis stream starts reading from this ID.
            // Useful for resuming from where you left off.
            // The `after_read` event provides `lastMessageId`,
            // which can be used as `start_id` to continue reading.
            'start_id' => '0'
        ],
        'events' => [
            // If set, this callback executes before `fclose` is called on the stream.
            // No need to close the Redis connection manually—the wrapper handles it.
            'before_stream_close' => function (string $redisKeyToStream, Redis $redis) {
                // Perform any actions before closing the stream, such as setting an expiration
                // or deleting the stream.
                //
                // Example: Set the key to expire after 60 seconds
                // $redis->expire($redisKeyToStream, 60);
            },
            // If set, this callback executes after reading from the stream.
            // Useful for retrieving the last message ID, which is needed
            // to continue reading from that point onward.
            'after_read' => function (string $lastMessageId, string $redisKeyToStream, Redis $redis) {
                //
            }
        ]
    ],
]
```

#### With Laravel

[](#with-laravel)

```
use Illuminate\Support\Facades\Redis as LaravelRedisFacade;

// Default Redis connection
$options = [
    'redis' => [
        'client' => function (): Redis {
            return LaravelRedisFacade::client();
        },
        // ...
    ],
];

// Specific Redis connection
$options = [
    'redis' => [
        'client' => function (): Redis {
            return LaravelRedisFacade::connection('cache')
                ->client();
        },
        // ...
    ],
];
```

#### Passing the options to the stream

[](#passing-the-options-to-the-stream)

```
// Global configuration

stream_context_set_default($options);

$handle = fopen('redis://important/streams/magic.txt', 'w');

// Per wrapper configuration

$context = stream_context_create($options)

$handle = fopen('redis://important/streams/magic.txt', 'w', $context);
```

### Writing to a Redis Stream

[](#writing-to-a-redis-stream)

```
$stream = fopen("redis://test", "w");
fwrite($stream, 'Hello, Redis!'));
fclose($stream);
```

### Reading from a Redis Stream

[](#reading-from-a-redis-stream)

```
$stream = fopen("redis://test", "r");
while (($data = fgets($stream)) !== false) {
    echo "Received: " . $data;
}
fclose($stream);
```

### License

[](#license)

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.

###  Health Score

26

—

LowBetter than 40% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

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 ~4 days

Total

2

Last Release

556d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1844873?v=4)[Mathieu Tanguay](/maintainers/lunfel)[@lunfel](https://github.com/lunfel)

---

Top Contributors

[![lunfel](https://avatars.githubusercontent.com/u/1844873?v=4)](https://github.com/lunfel "lunfel (16 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lunfel-redis-stream-wrapper/health.svg)

```
[![Health](https://phpackages.com/badges/lunfel-redis-stream-wrapper/health.svg)](https://phpackages.com/packages/lunfel-redis-stream-wrapper)
```

###  Alternatives

[iazaran/smart-cache

Smart Cache is a caching optimization package designed to enhance the way your Laravel application handles data caching. It intelligently manages large data sets by compressing, chunking, or applying other optimization strategies to keep your application performant and efficient.

21114.2k](/packages/iazaran-smart-cache)[ichhabrecht/intcache

Turn uncachable page objects into cacheable links

193.9k](/packages/ichhabrecht-intcache)

PHPackages © 2026

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