@@ -50,6 +50,8 @@ each feature.
5050| Feature | ` ioredis ` | ` node-redis ` |
5151| :-- | :-- | :-- |
5252| [ Pipelining] ( #pipelining ) | Automatic, or with ` pipeline() ` command | Automatic, or with ` multi() ` command |
53+ | [ Scan iteration] ( #scan-iteration ) | Uses ` scanStream() ` , etc. | Uses ` scanIterator() ` , etc |
54+ | [ Subscribing to channels] ( #subscribing-to-channels ) | Uses ` client.on('message', ...) ` event | Uses ` subscribe(...) ` command |
5355
5456### Specific commands
5557
@@ -201,16 +203,15 @@ commands, ending with `exec()` to run the pipeline:
201203
202204``` js
203205// ioredis example
204- redis
205- .pipeline ()
206- .set (" foo" , " 1" )
207- .get (" foo" )
208- .set (" foo" , " 2" )
209- .incr (" foo" )
210- .get (" foo" )
211- .exec (function (err , results ) {
212- // Handle results or errors.
213- });
206+ redis .pipeline ()
207+ .set (" foo" , " 1" )
208+ .get (" foo" )
209+ .set (" foo" , " 2" )
210+ .incr (" foo" )
211+ .get (" foo" )
212+ .exec (function (err , results ) {
213+ // Handle results or errors.
214+ });
214215```
215216
216217For ` node-redis ` , the approach is similar, except that you call the ` multi() `
@@ -230,6 +231,74 @@ redis.multi()
230231 });
231232```
232233
234+ ### Scan iteration
235+
236+ ` ioredis ` supports the ` scanStream() ` method to create a readable stream
237+ from the set of keys returned by the [ ` SCAN ` ] ({{< relref "/commands/scan" >}})
238+ command:
239+
240+ ``` js
241+ const redis = new Redis ();
242+ // Create a readable stream (object mode)
243+ const stream = redis .scanStream ();
244+ stream .on (" data" , (resultKeys ) => {
245+ // `resultKeys` is an array of strings representing key names.
246+ // Note that resultKeys may contain 0 keys, and that it will sometimes
247+ // contain duplicates due to SCAN's implementation in Redis.
248+ for (let i = 0 ; i < resultKeys .length ; i++ ) {
249+ console .log (resultKeys[i]);
250+ }
251+ });
252+ stream .on (" end" , () => {
253+ console .log (" all keys have been visited" );
254+ });
255+ ```
256+
257+ You can also use the similar ` hscanStream() ` , ` sscanStream() ` , and
258+ ` zscanStream() ` to iterate over the items of a hash, set, or sorted set,
259+ respectively.
260+
261+ ` node-redis ` handles scan iteration using the ` scanIterator() ` method
262+ (and the corresponding ` hscanIterator() ` , ` sscanIterator() ` , and
263+ ` zscanIterator() ` methods). These return a collection object for
264+ each page scanned by the cursor (this can be helpful to improve
265+ efficiency using [ ` MGET ` ] ({{< relref "/commands/mget" >}}) and
266+ other multi-key commands):
267+
268+ ``` js
269+ for await (const keys of client .scanIterator ()) {
270+ const values = await client .mGet (keys);
271+ }
272+ ```
273+
274+ ### Subscribing to channels
275+
276+ ` ioredis ` reports incoming pub/sub messages with a ` message `
277+ event on the client object (see
278+ [ Publish/subscribe] ({{< relref "/develop/interact/pubsub" >}}) for more
279+ information about messages):
280+
281+ ``` js
282+ client .on (' message' , (channel , message ) => {
283+ console .log (Received message from ${channel}: ${message});
284+ });
285+ ```
286+
287+ With ` node-redis ` , you use the ` subscribe() ` command to register the
288+ message callback. Also, when you use a connection to subscribe, the
289+ connection can't issue any other commands, so you must create a
290+ dedicated connection for the subscription. Use the ` client.duplicate() `
291+ method to create a new connection with the same settings as the original:
292+
293+ ``` js
294+ const subscriber = client .duplicate ();
295+ await subscriber .connect ();
296+
297+ await subscriber .subscribe (' channel' , (message ) => {
298+ console .log (Received message: ${message});
299+ });
300+ ```
301+
233302### ` SETNX ` command
234303
235304` ioredis ` implements the [ ` SETNX ` ] ({{< relref "/commands/setnx" >}})
@@ -245,4 +314,4 @@ command:
245314
246315``` js
247316await client .set (" bike:1" , " bike" , {' NX' : true });
248- ```
317+ ```
0 commit comments