@@ -33,12 +33,12 @@ npm install redis
33
33
const redis = require (" redis" );
34
34
const client = redis .createClient ();
35
35
36
- client .on (" error" , function (err ) {
37
- console .log ( " Error " + err );
36
+ client .on (" error" , function (error ) {
37
+ console .error (error );
38
38
});
39
39
40
- client .set (" string key" , " string val " , redis .print );
41
- client .get (" string key" , redis .print );
40
+ client .set (" key" , " value " , redis .print );
41
+ client .get (" key" , redis .print );
42
42
```
43
43
44
44
Note that the API is entirely asynchronous. To get data back from the server,
@@ -66,15 +66,17 @@ a variable number of individual arguments followed by an optional callback.
66
66
Examples:
67
67
68
68
``` js
69
- client .hmset ([" key" , " test keys 1 " , " test val 1 " , " test keys 2 " , " test val 2 " ], function (err , res ) {
69
+ client .hmset ([" key" , " foo " , " bar " ], function (err , res ) {
70
70
// ...
71
71
});
72
+
72
73
// Works the same as
73
- client .hmset (" key" , [" test keys 1 " , " test val 1 " , " test keys 2 " , " test val 2 " ], function (err , res ) {
74
+ client .hmset (" key" , [" foo " , " bar " ], function (err , res ) {
74
75
// ...
75
76
});
77
+
76
78
// Or
77
- client .hmset (" key" , " test keys 1 " , " test val 1 " , " test keys 2 " , " test val 2 " , function (err , res ) {
79
+ client .hmset (" key" , " foo " , " bar " , function (err , res ) {
78
80
// ...
79
81
});
80
82
```
@@ -84,15 +86,15 @@ Care should be taken with user input if arrays are possible (via body-parser, qu
84
86
Note that in either form the ` callback ` is optional:
85
87
86
88
``` js
87
- client .set (" some key " , " some val " );
88
- client .set ([" some other key " , " some val " ]);
89
+ client .set (" foo " , " bar " );
90
+ client .set ([" hello " , " world " ]);
89
91
```
90
92
91
93
If the key is missing, reply will be null. Only if the [ Redis Command
92
94
Reference] ( http://redis.io/commands ) states something else it will not be null.
93
95
94
96
``` js
95
- client .get (" missingkey " , function (err , reply ) {
97
+ client .get (" missing_key " , function (err , reply ) {
96
98
// reply is null when the key is missing
97
99
console .log (reply);
98
100
});
@@ -110,23 +112,23 @@ and Boolean values will result in the value coerced to a string!
110
112
111
113
` client ` will emit some events about the state of the connection to the Redis server.
112
114
113
- #### "ready"
115
+ #### ` "ready" `
114
116
115
117
` client ` will emit ` ready ` once a connection is established. Commands issued
116
118
before the ` ready ` event are queued, then replayed just before this event is
117
119
emitted.
118
120
119
- #### "connect"
121
+ #### ` "connect" `
120
122
121
123
` client ` will emit ` connect ` as soon as the stream is connected to the server.
122
124
123
- #### "reconnecting"
125
+ #### ` "reconnecting" `
124
126
125
127
` client ` will emit ` reconnecting ` when trying to reconnect to the Redis server
126
128
after losing the connection. Listeners are passed an object containing ` delay `
127
129
(in ms from the previous try) and ` attempt ` (the attempt #) attributes.
128
130
129
- #### "error"
131
+ #### ` "error" `
130
132
131
133
` client ` will emit ` error ` when encountering an error connecting to the Redis
132
134
server or when any other in node_redis occurs. If you use a command without
@@ -135,11 +137,11 @@ listener.
135
137
136
138
So please attach the error listener to node_redis.
137
139
138
- #### "end"
140
+ #### ` "end" `
139
141
140
142
` client ` will emit ` end ` when an established Redis server connection has closed.
141
143
142
- #### "warning"
144
+ #### ` "warning" `
143
145
144
146
` client ` will emit ` warning ` when password was set but none is needed and if a
145
147
deprecated option / function / similar is used.
@@ -390,184 +392,173 @@ syntax.
390
392
** Example:**
391
393
392
394
``` js
393
- client .hmset (" hosts" , " mjr" , " 1" , " another" , " 23" , " home" , " 1234" );
394
- client .hgetall (" hosts" , function (err , obj ) {
395
- console .dir (obj);
395
+ client .hmset (" key" , " foo" , " bar" , " hello" , " world" );
396
+
397
+ client .hgetall (" hosts" , function (err , value ) {
398
+ console .log (value .foo ); // > "bar"
399
+ console .log (value .hello ); // > "world"
396
400
});
397
401
```
398
402
399
- #### client.hmset(hash, obj [ , callback] )
403
+ #### client.hmset(hash, key1, val1, ...keyN, valN, [ callback] )
400
404
401
- Multiple values in a hash can be set by supplying an object .
405
+ Multiple values may also be set by supplying more arguments .
402
406
403
407
** Example:**
404
408
405
409
``` js
406
- client . HMSET (key2, {
407
- " 0123456789 " : " abcdefghij " , // NOTE: key and value will be coerced to strings
408
- " some manner of key " : " a type of value " ,
409
- } );
410
+ // key
411
+ // 1) foo => bar
412
+ // 2) hello => world
413
+ client . HMSET ( " key " , " foo " , " bar " , " hello " , " world " );
410
414
```
411
415
412
- The properties and values of this Object will be set as keys and values in the
413
- Redis hash.
416
+ ### PubSub
414
417
415
- #### client.hmset(hash, key1, val1, ...keyN, valN, [ callback] )
416
-
417
- Multiple values may also be set by supplying more arguments.
418
+ #### Example
418
419
419
- ** Example:**
420
+ This example opens two client connections, subscribes to a channel on one of them, and publishes to that
421
+ channel on the other.
420
422
421
423
``` js
422
- client .HMSET (key1, " 0123456789" , " abcdefghij" , " some manner of key" , " a type of value" );
423
- ```
424
+ const redis = require (" redis" );
424
425
425
- ### Publish / Subscribe
426
+ const subscriber = redis .createClient ();
427
+ const publisher = redis .createClient ();
426
428
427
- Example of the publish / subscribe API. This program opens two
428
- client connections, subscribes to a channel on one of them, and publishes to that
429
- channel on the other:
429
+ let messageCount = 0 ;
430
430
431
- ``` js
432
- var redis = require (" redis" );
433
- var sub = redis .createClient (),
434
- pub = redis .createClient ();
435
- var msg_count = 0 ;
436
-
437
- sub .on (" subscribe" , function (channel , count ) {
438
- pub .publish (" a nice channel" , " I am sending a message." );
439
- pub .publish (" a nice channel" , " I am sending a second message." );
440
- pub .publish (" a nice channel" , " I am sending my last message." );
431
+ subscriber .on (" subscribe" , function (channel , count ) {
432
+ publisher .publish (" a channel" , " a message" );
433
+ publisher .publish (" a channel" , " another message" );
441
434
});
442
435
443
- sub .on (" message" , function (channel , message ) {
444
- console .log (" sub channel " + channel + " : " + message);
445
- msg_count += 1 ;
446
- if (msg_count === 3 ) {
447
- sub .unsubscribe ();
448
- sub .quit ();
449
- pub .quit ();
436
+ subscriber .on (" message" , function (channel , message ) {
437
+ messageCount += 1 ;
438
+
439
+ console .log (" Subscriber received message in channel '" + channel + " ': " + message);
440
+
441
+ if (messageCount === 2 ) {
442
+ subscriber .unsubscribe ();
443
+ subscriber .quit ();
444
+ publisher .quit ();
450
445
}
451
446
});
452
447
453
- sub .subscribe (" a nice channel" );
448
+ subscriber .subscribe (" a channel" );
454
449
```
455
450
456
451
When a client issues a ` SUBSCRIBE ` or ` PSUBSCRIBE ` , that connection is put into
457
- a "subscriber" mode. At that point, the only valid commands are those that modify the subscription
452
+ a ` "subscriber" ` mode. At that point, the only valid commands are those that modify the subscription
458
453
set, and quit (also ping on some redis versions). When
459
454
the subscription set is empty, the connection is put back into regular mode.
460
455
461
456
If you need to send regular commands to Redis while in subscriber mode, just
462
- open another connection with a new client (hint: use ` client.duplicate() ` ).
457
+ open another connection with a new client (use ` client.duplicate() ` to quickly duplicate an existing client ).
463
458
464
- ## Subscriber Events
459
+ #### Subscriber Events
465
460
466
461
If a client has subscriptions active, it may emit these events:
467
462
468
- ### "message" (channel, message)
463
+ ** "message" (channel, message)** :
469
464
470
465
Client will emit ` message ` for every message received that matches an active subscription.
471
466
Listeners are passed the channel name as ` channel ` and the message as ` message ` .
472
467
473
- ### "pmessage" (pattern, channel, message)
468
+ ** "pmessage" (pattern, channel, message)** :
474
469
475
470
Client will emit ` pmessage ` for every message received that matches an active
476
471
subscription pattern. Listeners are passed the original pattern used with
477
472
` PSUBSCRIBE ` as ` pattern ` , the sending channel name as ` channel ` , and the
478
473
message as ` message ` .
479
474
480
- ### "message_buffer" (channel, message)
475
+ ** "message_buffer" (channel, message)** :
481
476
482
477
This is the same as the ` message ` event with the exception, that it is always
483
478
going to emit a buffer. If you listen to the ` message ` event at the same time as
484
479
the ` message_buffer ` , it is always going to emit a string.
485
480
486
- ### "pmessage_buffer" (pattern, channel, message)
481
+ ** "pmessage_buffer" (pattern, channel, message)** :
487
482
488
483
This is the same as the ` pmessage ` event with the exception, that it is always
489
484
going to emit a buffer. If you listen to the ` pmessage ` event at the same time
490
485
as the ` pmessage_buffer ` , it is always going to emit a string.
491
486
492
- ### "subscribe" (channel, count)
487
+ ** "subscribe" (channel, count)** :
493
488
494
489
Client will emit ` subscribe ` in response to a ` SUBSCRIBE ` command. Listeners are
495
490
passed the channel name as ` channel ` and the new count of subscriptions for this
496
491
client as ` count ` .
497
492
498
- ### "psubscribe" (pattern, count)
493
+ ** "psubscribe" (pattern, count)** :
499
494
500
495
Client will emit ` psubscribe ` in response to a ` PSUBSCRIBE ` command. Listeners
501
496
are passed the original pattern as ` pattern ` , and the new count of subscriptions
502
497
for this client as ` count ` .
503
498
504
- ### "unsubscribe" (channel, count)
499
+ ** "unsubscribe" (channel, count)** :
505
500
506
501
Client will emit ` unsubscribe ` in response to a ` UNSUBSCRIBE ` command. Listeners
507
502
are passed the channel name as ` channel ` and the new count of subscriptions for
508
503
this client as ` count ` . When ` count ` is 0, this client has left subscriber mode
509
504
and no more subscriber events will be emitted.
510
505
511
- ### "punsubscribe" (pattern, count)
506
+ ** "punsubscribe" (pattern, count)** :
512
507
513
508
Client will emit ` punsubscribe ` in response to a ` PUNSUBSCRIBE ` command.
514
509
Listeners are passed the channel name as ` channel ` and the new count of
515
510
subscriptions for this client as ` count ` . When ` count ` is 0, this client has
516
511
left subscriber mode and no more subscriber events will be emitted.
517
512
518
- ## client.multi([ commands] )
513
+ ### client.multi([ commands] )
519
514
520
515
` MULTI ` commands are queued up until an ` EXEC ` is issued, and then all commands
521
516
are run atomically by Redis. The interface in ` node_redis ` is to return an
522
517
individual ` Multi ` object by calling ` client.multi() ` . If any command fails to
523
518
queue, all commands are rolled back and none is going to be executed (For
524
- further information look at
525
- [ transactions] ( http://redis.io/topics/transactions ) ).
519
+ further information see the [ Redis transactions] ( http://redis.io/topics/transactions ) documentation).
526
520
527
521
``` js
528
- var redis = require (" ./index" ),
529
- client = redis .createClient (),
530
- set_size = 20 ;
522
+ const redis = require (" redis" );
523
+ const client = redis .createClient ();
524
+
525
+ let setSize = 20 ;
531
526
532
- client .sadd (" bigset " , " a member " );
533
- client .sadd (" bigset " , " another member " );
527
+ client .sadd (" key " , " member1 " );
528
+ client .sadd (" key " , " member2 " );
534
529
535
- while (set_size > 0 ) {
536
- client .sadd (" bigset " , " member " + set_size );
537
- set_size -= 1 ;
530
+ while (setSize > 0 ) {
531
+ client .sadd (" key " , " member" + setSize );
532
+ setSize -= 1 ;
538
533
}
539
534
540
- // multi chain with an individual callback
535
+ // chain commands
541
536
client
542
537
.multi ()
543
- .scard (" bigset" )
544
- .smembers (" bigset" )
545
- .keys (" *" , function (err , replies ) {
546
- // NOTE: code in this callback is NOT atomic
547
- // this only happens after the the .exec call finishes.
548
- client .mget (replies, redis .print );
549
- })
538
+ .scard (" key" )
539
+ .smembers (" key" )
540
+ .keys (" *" )
550
541
.dbsize ()
551
542
.exec (function (err , replies ) {
552
543
console .log (" MULTI got " + replies .length + " replies" );
553
544
replies .forEach (function (reply , index ) {
554
- console .log (" Reply " + index + " : " + reply .toString ());
545
+ console .log (" REPLY @ index " + index + " : " + reply .toString ());
555
546
});
556
547
});
557
548
```
558
549
559
- ### Multi.exec([ callback] )
550
+ #### Multi.exec([ callback] )
560
551
561
552
` client.multi() ` is a constructor that returns a ` Multi ` object. ` Multi ` objects
562
553
share all of the same command methods as ` client ` objects do. Commands are
563
554
queued up inside the ` Multi ` object until ` Multi.exec() ` is invoked.
564
555
565
- If your code contains an syntax error an EXECABORT error is going to be thrown
556
+ If your code contains an syntax error an ` EXECABORT ` error is going to be thrown
566
557
and all commands are going to be aborted. That error contains a ` .errors `
567
558
property that contains the concrete errors.
568
559
If all commands were queued successfully and an error is thrown by redis while
569
560
processing the commands that error is going to be returned in the result array!
570
- No other command is going to be aborted though than the onces failing.
561
+ No other command is going to be aborted though than the ones failing.
571
562
572
563
You can either chain together ` MULTI ` commands as in the above example, or you
573
564
can queue individual commands while still sending regular client command as in
0 commit comments