@@ -33,12 +33,12 @@ npm install redis
3333const redis = require (" redis" );
3434const client = redis .createClient ();
3535
36- client .on (" error" , function (err ) {
37- console .log ( " Error " + err );
36+ client .on (" error" , function (error ) {
37+ console .error (error );
3838});
3939
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 );
4242```
4343
4444Note 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.
6666Examples:
6767
6868``` 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 ) {
7070 // ...
7171});
72+
7273// 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 ) {
7475 // ...
7576});
77+
7678// 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 ) {
7880 // ...
7981});
8082```
@@ -84,15 +86,15 @@ Care should be taken with user input if arrays are possible (via body-parser, qu
8486Note that in either form the ` callback ` is optional:
8587
8688``` 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 " ]);
8991```
9092
9193If the key is missing, reply will be null. Only if the [ Redis Command
9294Reference] ( http://redis.io/commands ) states something else it will not be null.
9395
9496``` js
95- client .get (" missingkey " , function (err , reply ) {
97+ client .get (" missing_key " , function (err , reply ) {
9698 // reply is null when the key is missing
9799 console .log (reply);
98100});
@@ -110,23 +112,23 @@ and Boolean values will result in the value coerced to a string!
110112
111113` client ` will emit some events about the state of the connection to the Redis server.
112114
113- #### "ready"
115+ #### ` "ready" `
114116
115117` client ` will emit ` ready ` once a connection is established. Commands issued
116118before the ` ready ` event are queued, then replayed just before this event is
117119emitted.
118120
119- #### "connect"
121+ #### ` "connect" `
120122
121123` client ` will emit ` connect ` as soon as the stream is connected to the server.
122124
123- #### "reconnecting"
125+ #### ` "reconnecting" `
124126
125127` client ` will emit ` reconnecting ` when trying to reconnect to the Redis server
126128after losing the connection. Listeners are passed an object containing ` delay `
127129(in ms from the previous try) and ` attempt ` (the attempt #) attributes.
128130
129- #### "error"
131+ #### ` "error" `
130132
131133` client ` will emit ` error ` when encountering an error connecting to the Redis
132134server or when any other in node_redis occurs. If you use a command without
@@ -135,11 +137,11 @@ listener.
135137
136138So please attach the error listener to node_redis.
137139
138- #### "end"
140+ #### ` "end" `
139141
140142` client ` will emit ` end ` when an established Redis server connection has closed.
141143
142- #### "warning"
144+ #### ` "warning" `
143145
144146` client ` will emit ` warning ` when password was set but none is needed and if a
145147deprecated option / function / similar is used.
@@ -390,184 +392,173 @@ syntax.
390392** Example:**
391393
392394``` 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"
396400});
397401```
398402
399- #### client.hmset(hash, obj [ , callback] )
403+ #### client.hmset(hash, key1, val1, ...keyN, valN, [ callback] )
400404
401- Multiple values in a hash can be set by supplying an object .
405+ Multiple values may also be set by supplying more arguments .
402406
403407** Example:**
404408
405409``` 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 " );
410414```
411415
412- The properties and values of this Object will be set as keys and values in the
413- Redis hash.
416+ ### PubSub
414417
415- #### client.hmset(hash, key1, val1, ...keyN, valN, [ callback] )
416-
417- Multiple values may also be set by supplying more arguments.
418+ #### Example
418419
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.
420422
421423``` js
422- client .HMSET (key1, " 0123456789" , " abcdefghij" , " some manner of key" , " a type of value" );
423- ```
424+ const redis = require (" redis" );
424425
425- ### Publish / Subscribe
426+ const subscriber = redis .createClient ();
427+ const publisher = redis .createClient ();
426428
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 ;
430430
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" );
441434});
442435
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 ();
450445 }
451446});
452447
453- sub .subscribe (" a nice channel" );
448+ subscriber .subscribe (" a channel" );
454449```
455450
456451When 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
458453set, and quit (also ping on some redis versions). When
459454the subscription set is empty, the connection is put back into regular mode.
460455
461456If 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 ).
463458
464- ## Subscriber Events
459+ #### Subscriber Events
465460
466461If a client has subscriptions active, it may emit these events:
467462
468- ### "message" (channel, message)
463+ ** "message" (channel, message)** :
469464
470465Client will emit ` message ` for every message received that matches an active subscription.
471466Listeners are passed the channel name as ` channel ` and the message as ` message ` .
472467
473- ### "pmessage" (pattern, channel, message)
468+ ** "pmessage" (pattern, channel, message)** :
474469
475470Client will emit ` pmessage ` for every message received that matches an active
476471subscription pattern. Listeners are passed the original pattern used with
477472` PSUBSCRIBE ` as ` pattern ` , the sending channel name as ` channel ` , and the
478473message as ` message ` .
479474
480- ### "message_buffer" (channel, message)
475+ ** "message_buffer" (channel, message)** :
481476
482477This is the same as the ` message ` event with the exception, that it is always
483478going to emit a buffer. If you listen to the ` message ` event at the same time as
484479the ` message_buffer ` , it is always going to emit a string.
485480
486- ### "pmessage_buffer" (pattern, channel, message)
481+ ** "pmessage_buffer" (pattern, channel, message)** :
487482
488483This is the same as the ` pmessage ` event with the exception, that it is always
489484going to emit a buffer. If you listen to the ` pmessage ` event at the same time
490485as the ` pmessage_buffer ` , it is always going to emit a string.
491486
492- ### "subscribe" (channel, count)
487+ ** "subscribe" (channel, count)** :
493488
494489Client will emit ` subscribe ` in response to a ` SUBSCRIBE ` command. Listeners are
495490passed the channel name as ` channel ` and the new count of subscriptions for this
496491client as ` count ` .
497492
498- ### "psubscribe" (pattern, count)
493+ ** "psubscribe" (pattern, count)** :
499494
500495Client will emit ` psubscribe ` in response to a ` PSUBSCRIBE ` command. Listeners
501496are passed the original pattern as ` pattern ` , and the new count of subscriptions
502497for this client as ` count ` .
503498
504- ### "unsubscribe" (channel, count)
499+ ** "unsubscribe" (channel, count)** :
505500
506501Client will emit ` unsubscribe ` in response to a ` UNSUBSCRIBE ` command. Listeners
507502are passed the channel name as ` channel ` and the new count of subscriptions for
508503this client as ` count ` . When ` count ` is 0, this client has left subscriber mode
509504and no more subscriber events will be emitted.
510505
511- ### "punsubscribe" (pattern, count)
506+ ** "punsubscribe" (pattern, count)** :
512507
513508Client will emit ` punsubscribe ` in response to a ` PUNSUBSCRIBE ` command.
514509Listeners are passed the channel name as ` channel ` and the new count of
515510subscriptions for this client as ` count ` . When ` count ` is 0, this client has
516511left subscriber mode and no more subscriber events will be emitted.
517512
518- ## client.multi([ commands] )
513+ ### client.multi([ commands] )
519514
520515` MULTI ` commands are queued up until an ` EXEC ` is issued, and then all commands
521516are run atomically by Redis. The interface in ` node_redis ` is to return an
522517individual ` Multi ` object by calling ` client.multi() ` . If any command fails to
523518queue, 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).
526520
527521``` 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 ;
531526
532- client .sadd (" bigset " , " a member " );
533- client .sadd (" bigset " , " another member " );
527+ client .sadd (" key " , " member1 " );
528+ client .sadd (" key " , " member2 " );
534529
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 ;
538533}
539534
540- // multi chain with an individual callback
535+ // chain commands
541536client
542537 .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 (" *" )
550541 .dbsize ()
551542 .exec (function (err , replies ) {
552543 console .log (" MULTI got " + replies .length + " replies" );
553544 replies .forEach (function (reply , index ) {
554- console .log (" Reply " + index + " : " + reply .toString ());
545+ console .log (" REPLY @ index " + index + " : " + reply .toString ());
555546 });
556547 });
557548```
558549
559- ### Multi.exec([ callback] )
550+ #### Multi.exec([ callback] )
560551
561552` client.multi() ` is a constructor that returns a ` Multi ` object. ` Multi ` objects
562553share all of the same command methods as ` client ` objects do. Commands are
563554queued up inside the ` Multi ` object until ` Multi.exec() ` is invoked.
564555
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
566557and all commands are going to be aborted. That error contains a ` .errors `
567558property that contains the concrete errors.
568559If all commands were queued successfully and an error is thrown by redis while
569560processing 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.
571562
572563You can either chain together ` MULTI ` commands as in the above example, or you
573564can queue individual commands while still sending regular client command as in
0 commit comments