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)