I have a NodeJS application that is using Redis stream (library ‘ioredis’) to pass information around. The problem is that when I add a message to a stream and I try to retrieve it, I have to go down a lot of Arrays level:
const message = await redis.xreadgroup('GROUP', orderGroup, orderConsumer, 'COUNT', 1, 'STREAMS', orderStream, '>');
const messageId: string = message[0][1][0][0];
const pMsg: Obj = JSON.parse(JSON.parse(message[0][1][0][1][1]));
This is how I create the stream:
await redis.xgroup('CREATE', orderStream, orderGroup, '0', 'MKSTREAM')
.catch((err) => {
console.error(`Group already exists error: ${err}`);
})
Is this normal? In the Redis doc (https://redis.io/commands/xreadgroup) it shows that the return value is an array with the id of the message at position 0 and the fields at position 1. I feel like I’m missing something…
3
Answers
Here is an example output of XREADGROUP, as you can see the values are at the nested level 5.
For more details see https://redis.io/commands/xread
It is normal and expected.
XREADGROUP
supports reading from multiples stream keys, multiple messages, and messages can have multiple field-value pairs.Follow the next example:
The structure you get has multiple nested arrays. Using 0-indexed as in node:
Where
data[0][1]
is the root level array (adjust this entry point for your own use).Variables
rd
: Return Datael
: Elementsel
: Sub-elementrel
: Relative-elementp
: Relative-Objectc
: Iterate-Counter for deciding if it is a key or value.