clue/reactphp-redis

Redis Streams example?

Closed this issue · 4 comments

Is there an example of using this library with the Streams data structure introduced in Redis v5?

clue commented

@smaddock The new streams are supported like any other Redis command. This library supports all Redis commands as per https://github.com/clue/reactphp-redis#commands.

For example, the following command

XADD mystream * item 1

maps directory to

$client->xadd('mystream', '*', 'item', '1');

I hope this helps 👍

I should have been more specific. How would one use this library to loop through XREADGROUP using the ReactPHP event loop instead of an infinite while loop as shown at https://redis.io/commands/xreadgroup#usage-example ? This seems to be built out for Pub/Sub using a callback function but I am unclear how to add $client->xreadgroup(); as a repeating event listener.

clue commented

Here's an excerpt from another application I've built that uses the RPOP command which has very similar semantics to the XREADGROUP command:

class ListReader extends ReadableStreamInterface
{
    …

    private function poll()
    {
        $this->client->rpop($this->list)->then(function ($data) {
            if ($data === null && !$this->closed) {
                $this->emit('end');
                $this->close();
            }
            if ($data !== null && !$this->closed) {
                $this->emit('data', [$data]);
            }

            if (!$this->paused && !$this->closed) {
                $this->poll();
            }
        }, function (Exception $e) {
            $this->emit('error', [$e]);
            $this->close();
        });
    }
}

I hope this helps to get you started 👍

(For more specific use cases, you can always shoot me a mail to discuss possible support options.)

Yes!!! Thank you!