Node readable and writable streams for redis list
Readable stream read all list elements using redis lpop. When there is no more elements in the list, stream emits an end event.
Writable stream push elements at the end of the list using redis rpush. When all elements are pushed, stream emit a finish event.
In this sample you will see how to write object and read object from a list
const writeStream = new Writable({
"redisClient": redisClient,
"listKey": "sample-list-key",
"objectMode": true
});
const input = new PassThrough({"objectMode": true});
input.write({"data": "data1"});
input.write({"data": "data1"});
input.push(null);
input.pipe(writeStream).on("finish", () => {
process.stdout.write("write finish\n");
const readStream = new Readable({
"redisClient": redisClient,
"objectMode": true,
"listKey": "sample-list-key"
});
readStream.on("data", (data) => {
process.stdout.write("data: " + JSON.stringify(data) + "\n");
});
readStream.on("end", () => {
process.stdout.write("read end\n");
redisClient.quit();
});
});