0

I have a redis stream "mystream" and I am trying to use:

XREAD COUNT 5 BLOCK 50000 STREAMS mystream $

to read five new entries appended to mystream with blocking for 50s. As soon as I execute command to add data to mystream using:

XADD mystream a 5 b 6

the blocked command exits with giving single entry as the output.

Is there any way of XREAD command using which I can receive multiple entries appended from now on till 50s or do I have to call XREAD multiple times for achieving the same ?

Pranav Gupta
  • 651
  • 9
  • 14

1 Answers1

0

What you are asking for is something that is not meant for usage of XREAD BLOCK. I would suggest the XREVRANGE command:

xrevrange mystream + - COUNT 5

min: "+" - indicates the highest id of the stream and max: "-" - indicates the lowest id of the stream. Instead of "-" you could also pass a TIMESTAMP which would limit your XREVRANGE results to a desired time to TIMESTAMP seconds ago (inclusive).

xrevrange mystream + 1596617691455 COUNT 5

The suggestion above is not achieving the same blocking behavior. If you would still like to react to incoming data you might look into Redis Pub/Sub. Subscribe to a topic, and write your own logic to handle incoming data. There is a webchat example.

You could also use XREAD BLOCK to detect the min value of your XREVRANGE command:

XREAD COUNT 5 BLOCK 50000 STREAMS mystream $
XREVRANGE mystream + [returned_xread_key] COUNT 5

I am sorry, but I cannot provide examples in the C language, here's in Python:

import redis
import time
redis_client = redis.StrictRedis("0.0.0.0", decode_responses=True)
response = redis_client.xread({'mystream': "$"}, count=1, block=50000)
if len(response) > 0:
    min_value = response[0][1][0][0] # [['mystream', [('1596618943439-0', {'mykey': 'myval'})]]]
    time.sleep(50) # wait for the end of the window
    entries = redis_client.xrevrange('mystream', max="+", min=min_value, count=5)
Jan Vilks
  • 1
  • 1