Added
xadd
, xlen
, xrange
, xrevrange
and xread
for stream support (#449 @critocrito)
Support basic stream operations (https://redis.io/topics/streams-intro) for
the upcoming Redis 5 release. Some features are missing, such as consumer
groups, capped streams or some of the auxiliary commands.
Event ids are auto incremented integers in the form of 1-0
, 2-0
,
etc. Events are stored internally as an array of arrays and for every event an
additional record is kept whether the event has been already polled or not.
const redis = new MockRedis({
mystream: [
["1-0", ["key1", "val1", "key2", "val2"]],
["2-0", ["key1", "val1", "key2", "val2"]]
],
"stream:mystream:1-0": {polled: false},
"stream:mystream:2-0": {polled: false},
})
To poll events in a blocking manner is supported and the poll happens every
100ms.
// This will resolve once a new event becomes available.
redis.xread("BLOCK", "0", "STREAMS", "mystream", "$").then(console.log);
// After 1 second add a new event, that will resolve the waiting `XREAD`
// command.
setTimeout(() => redis.xadd("mystream", "*", "key", "val"), 1000);
ioredis
doesn't support Redis streams yet, but there is pull request for redis-commands
to add support. In the meantime ioredis
' createBuiltinCommand
can be used to add support manually:
import Redis from "ioredis";
const {string: xadd} = Redis.prototype.createBuiltinCommand("xadd");
const {string: xread} = Redis.prototype.createBuiltinCommand("xread");
Redis.prototype.xadd = xadd;
Redis.prototype.xread = xread;
const redis = new Redis();