Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streaming server response #1179

Closed
aletorrado opened this issue Dec 14, 2016 · 7 comments
Closed

Streaming server response #1179

aletorrado opened this issue Dec 14, 2016 · 7 comments
Labels
Projects

Comments

@aletorrado
Copy link

Hi, is possible with node_redis to stream the server response with commands like HMGET or KEYS?

Thanks.

@hossam-fares
Copy link

👍 it would be great if we have implementation for it

@hossam-fares
Copy link

@aletorrado I already had similar situation and I wanted to stream from HKEYS cmd
so I created a readable stream using HSCAN cmd, with this implementation:

const redis = require('redis');
const connectionParams = {};

class HKeysReadable extends require('stream').Readable {
  constructor(key, chunkSize, opt) {
    opt = opt || {};
    opt.objectMode = true;
    super(opt);
    this._cursor = 0;
    this.key = key;
    this.chunkSize = chunkSize;
    this.client = redis.createClient(connectionParams);
  }

  _read() {
    if (this._cursor === '0') {
      this.push(null);
      return this.client.quit();
    }

    this.client.hscan(this.key, this._cursor, 'COUNT', this.chunkSize, (err, res) => {
        if (err) {
          this.push(null);
          this.client.quit();
          return process.nextTick(() => this.emit('error', err));
        }

        this._cursor = res[0];

        /**
         *
         SCAN returns value as an array of two values, the first value is the new cursor to use in the next call,
         the second value is an array of elements like: [key1, val1, key2, val2, key3, val3, .....]
         In this stream, we need the keys only so we reduce that array to get only the odd values which contain the keys

         */
        let keys = res[1].reduce((accumulator, val, index)=> {
          if (!(index % 2)) {
            accumulator.push(val);
          }

          return accumulator;
        }, []);

        if (keys.length > 0) {
          this.push(keys);
        }
      }
    );
  }
};

@BridgeAR what do you think? is there a possibility to adapt similar implementation in Redis library as well?

@aletorrado
Copy link
Author

Great @hossam-fares. Basically it may abstract cursors from node_redis clients, for a whitelisted set of redis commands (ie. SCAN, SSCAN, HSCAN, ZSCAN).

@BridgeAR
Copy link
Contributor

@hossam-fares I would definitely like to have something similar implemented 👍

@hossam-fares
Copy link

@BridgeAR cool, I will work on a PR for it

@revington
Copy link

Has this been implemented?

@leibale leibale added this to To do in v4 via automation Jul 1, 2021
@leibale
Copy link
Collaborator

leibale commented Jul 1, 2021

@aletorrado @hossam-fares @revington

I think that AsyncIterator is more suitable:

for await (const key of client.scanIterator()) {
  console.log(key);
}

what do you think?

@leibale leibale moved this from To do to Done in v4 Jul 8, 2021
@leibale leibale closed this as completed Nov 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
v4
Done
Development

No branches or pull requests

5 participants