UNPKG

39.6 kBMarkdownView Raw
1redis - a node.js redis client
2===========================
3
4[![Build Status](https://travis-ci.org/NodeRedis/node_redis.svg?branch=master)](https://travis-ci.org/NodeRedis/node_redis)
5[![Coverage Status](https://coveralls.io/repos/NodeRedis/node_redis/badge.svg?branch=)](https://coveralls.io/r/NodeRedis/node_redis?branch=)
6[![Windows Tests](https://img.shields.io/appveyor/ci/BridgeAR/node-redis/master.svg?label=Windows%20Tests)](https://ci.appveyor.com/project/BridgeAR/node-redis/branch/master)
7[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/NodeRedis/node_redis?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
8
9This is a complete and feature rich Redis client for node.js. __It supports all Redis commands__ and focuses on high performance.
10
11Install with:
12
13 npm install redis
14
15## Usage Example
16
17```js
18var redis = require("redis"),
19 client = redis.createClient();
20
21// if you'd like to select database 3, instead of 0 (default), call
22// client.select(3, function() { /* ... */ });
23
24client.on("error", function (err) {
25 console.log("Error " + err);
26});
27
28client.set("string key", "string val", redis.print);
29client.hset("hash key", "hashtest 1", "some value", redis.print);
30client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
31client.hkeys("hash key", function (err, replies) {
32 console.log(replies.length + " replies:");
33 replies.forEach(function (reply, i) {
34 console.log(" " + i + ": " + reply);
35 });
36 client.quit();
37});
38```
39
40This will display:
41
42 mjr:~/work/node_redis (master)$ node example.js
43 Reply: OK
44 Reply: 0
45 Reply: 0
46 2 replies:
47 0: hashtest 1
48 1: hashtest 2
49 mjr:~/work/node_redis (master)$
50
51Note that the API is entirely asynchronous. To get data back from the server, you'll need to use a callback.
52From v.2.6 on the API supports camelCase and snake_case and all options / variables / events etc. can be used either way.
53It is recommended to use camelCase as this is the default for the Node.js landscape.
54
55### Promises
56
57You can also use node_redis with promises by promisifying node_redis with [bluebird](https://github.com/petkaantonov/bluebird) as in:
58
59```js
60var redis = require('redis');
61bluebird.promisifyAll(redis.RedisClient.prototype);
62bluebird.promisifyAll(redis.Multi.prototype);
63```
64
65It'll add a *Async* to all node_redis functions (e.g. return client.getAsync().then())
66
67```js
68// We expect a value 'foo': 'bar' to be present
69// So instead of writing client.get('foo', cb); you have to write:
70return client.getAsync('foo').then(function(res) {
71 console.log(res); // => 'bar'
72});
73
74// Using multi with promises looks like:
75
76return client.multi().get('foo').execAsync().then(function(res) {
77 console.log(res); // => 'bar'
78});
79```
80
81### Sending Commands
82
83Each Redis command is exposed as a function on the `client` object.
84All functions take either an `args` Array plus optional `callback` Function or
85a variable number of individual arguments followed by an optional callback.
86Examples:
87
88```js
89client.hmset(["key", "test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {});
90// Works the same as
91client.hmset("key", ["test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {});
92// Or
93client.hmset("key", "test keys 1", "test val 1", "test keys 2", "test val 2", function (err, res) {});
94```
95
96Note that in either form the `callback` is optional:
97
98```js
99client.set("some key", "some val");
100client.set(["some other key", "some val"]);
101```
102
103If the key is missing, reply will be null. Only if the [Redis Command Reference](http://redis.io/commands) states something else it will not be null.
104
105```js
106client.get("missingkey", function(err, reply) {
107 // reply is null when the key is missing
108 console.log(reply);
109});
110```
111
112For a list of Redis commands, see [Redis Command Reference](http://redis.io/commands)
113
114Minimal parsing is done on the replies. Commands that return a integer return JavaScript Numbers, arrays return JavaScript Array. `HGETALL` returns an Object keyed by the hash keys. All strings will either be returned as string or as buffer depending on your setting.
115Please be aware that sending null, undefined and Boolean values will result in the value coerced to a string!
116
117# API
118
119## Connection and other Events
120
121`client` will emit some events about the state of the connection to the Redis server.
122
123### "ready"
124
125`client` will emit `ready` once a connection is established. Commands issued before the `ready` event are queued,
126then replayed just before this event is emitted.
127
128### "connect"
129
130`client` will emit `connect` as soon as the stream is connected to the server.
131
132### "reconnecting"
133
134`client` will emit `reconnecting` when trying to reconnect to the Redis server after losing the connection. Listeners
135are passed an object containing `delay` (in ms) and `attempt` (the attempt #) attributes.
136
137### "error"
138
139`client` will emit `error` when encountering an error connecting to the Redis server or when any other in node_redis occurs.
140If you use a command without callback and encounter a ReplyError it is going to be emitted to the error listener.
141
142So please attach the error listener to node_redis.
143
144### "end"
145
146`client` will emit `end` when an established Redis server connection has closed.
147
148### "drain" (deprecated)
149
150`client` will emit `drain` when the TCP connection to the Redis server has been buffering, but is now
151writable. This event can be used to stream commands in to Redis and adapt to backpressure.
152
153If the stream is buffering `client.should_buffer` is set to true. Otherwise the variable is always set to false.
154That way you can decide when to reduce your send rate and resume sending commands when you get `drain`.
155
156You can also check the return value of each command as it will also return the backpressure indicator (deprecated).
157If false is returned the stream had to buffer.
158
159### "warning"
160
161`client` will emit `warning` when password was set but none is needed and if a deprecated option / function / similar is used.
162
163### "idle" (deprecated)
164
165`client` will emit `idle` when there are no outstanding commands that are awaiting a response.
166
167## redis.createClient()
168If you have `redis-server` running on the same machine as node, then the defaults for
169port and host are probably fine and you don't need to supply any arguments. `createClient()` returns a `RedisClient` object. Otherwise, `createClient()` accepts these arguments:
170
171* `redis.createClient([options])`
172* `redis.createClient(unix_socket[, options])`
173* `redis.createClient(redis_url[, options])`
174* `redis.createClient(port[, host][, options])`
175
176__Tip:__ If the Redis server runs on the same machine as the client consider using unix sockets if possible to increase throughput.
177
178#### `options` object properties
179| Property | Default | Description |
180|-----------|-----------|-------------|
181| host | 127.0.0.1 | IP address of the Redis server |
182| port | 6379 | Port of the Redis server |
183| path | null | The UNIX socket string of the Redis server |
184| url | null | The URL of the Redis server. Format: `[redis:]//[[user][:password@]][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]` (More info avaliable at [IANA](http://www.iana.org/assignments/uri-schemes/prov/redis)). |
185| parser | javascript | __Deprecated__ Use either the built-in JS parser [`javascript`]() or the native [`hiredis`]() parser. __Note__ `node_redis` < 2.6 uses hiredis as default if installed. This changed in v.2.6.0. |
186| string_numbers | null | Set to `true`, `node_redis` will return Redis number values as Strings instead of javascript Numbers. Useful if you need to handle big numbers (above `Number.MAX_SAFE_INTEGER === 2^53`). Hiredis is incapable of this behavior, so setting this option to `true` will result in the built-in javascript parser being used no matter the value of the `parser` option. |
187| return_buffers | false | If set to `true`, then all replies will be sent to callbacks as Buffers instead of Strings. |
188| detect_buffers | false | If set to `true`, then replies will be sent to callbacks as Buffers. This option lets you switch between Buffers and Strings on a per-command basis, whereas `return_buffers` applies to every command on a client. __Note__: This doesn't work properly with the pubsub mode. A subscriber has to either always return Strings or Buffers. |
189| socket_keepalive | true | If set to `true`, the keep-alive functionality is enabled on the underlying socket. |
190| no_ready_check | false | When a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server will not respond to any commands. To work around this, `node_redis` has a "ready check" which sends the `INFO` command to the server. The response from the `INFO` command indicates whether the server is ready for more commands. When ready, `node_redis` emits a `ready` event. Setting `no_ready_check` to `true` will inhibit this check. |
191| enable_offline_queue | true | By default, if there is no active connection to the Redis server, commands are added to a queue and are executed once the connection has been established. Setting `enable_offline_queue` to `false` will disable this feature and the callback will be executed immediately with an error, or an error will be emitted if no callback is specified. |
192| retry_max_delay | null | __Deprecated__ _Please use `retry_strategy` instead._ By default, every time the client tries to connect and fails, the reconnection delay almost doubles. This delay normally grows infinitely, but setting `retry_max_delay` limits it to the maximum value provided in milliseconds. |
193| connect_timeout | 3600000 | __Deprecated__ _Please use `retry_strategy` instead._ Setting `connect_timeout` limits the total time for the client to connect and reconnect. The value is provided in milliseconds and is counted from the moment a new client is created or from the time the connection is lost. The last retry is going to happen exactly at the timeout time. Default is to try connecting until the default system socket timeout has been exceeded and to try reconnecting until 1h has elapsed. |
194| max_attempts | 0 | __Deprecated__ _Please use `retry_strategy` instead._ By default, a client will try reconnecting until connected. Setting `max_attempts` limits total amount of connection attempts. Setting this to 1 will prevent any reconnect attempt. |
195| retry_unfulfilled_commands | false | If set to `true`, all commands that were unfulfilled while the connection is lost will be retried after the connection has been reestablished. Use this with caution if you use state altering commands (e.g. `incr`). This is especially useful if you use blocking commands. |
196| password | null | If set, client will run Redis auth command on connect. Alias `auth_pass` __Note__ `node_redis` < 2.5 must use `auth_pass` |
197| db | null | If set, client will run Redis `select` command on connect. |
198| family | IPv4 | You can force using IPv6 if you set the family to 'IPv6'. See Node.js [net](https://nodejs.org/api/net.html) or [dns](https://nodejs.org/api/dns.html) modules on how to use the family type. |
199| disable_resubscribing | false | If set to `true`, a client won't resubscribe after disconnecting. |
200| rename_commands | null | Passing an object with renamed commands to use instead of the original functions. See the [Redis security topics](http://redis.io/topics/security) for more info. |
201| tls | null | An object containing options to pass to [tls.connect](http://nodejs.org/api/tls.html#tls_tls_connect_port_host_options_callback) to set up a TLS connection to Redis (if, for example, it is set up to be accessible via a tunnel). |
202| prefix | null | A string used to prefix all used keys (e.g. `namespace:test`). Please be aware that the `keys` command will not be prefixed. The `keys` command has a "pattern" as argument and no key and it would be impossible to determine the existing keys in Redis if this would be prefixed. |
203| retry_strategy | function | A function that receives an options object as parameter including the retry `attempt`, the `total_retry_time` indicating how much time passed since the last time connected, the `error` why the connection was lost and the number of `times_connected` in total. If you return a number from this function, the retry will happen exactly after that time in milliseconds. If you return a non-number, no further retry will happen and all offline commands are flushed with errors. Return an error to return that specific error to all offline commands. Example below. |
204
205```js
206var redis = require("redis");
207var client = redis.createClient({detect_buffers: true});
208
209client.set("foo_rand000000000000", "OK");
210
211// This will return a JavaScript String
212client.get("foo_rand000000000000", function (err, reply) {
213 console.log(reply.toString()); // Will print `OK`
214});
215
216// This will return a Buffer since original key is specified as a Buffer
217client.get(new Buffer("foo_rand000000000000"), function (err, reply) {
218 console.log(reply.toString()); // Will print `<Buffer 4f 4b>`
219});
220client.quit();
221```
222
223retry_strategy example
224```js
225var client = redis.createClient({
226 retry_strategy: function (options) {
227 if (options.error.code === 'ECONNREFUSED') {
228 // End reconnecting on a specific error and flush all commands with a individual error
229 return new Error('The server refused the connection');
230 }
231 if (options.total_retry_time > 1000 * 60 * 60) {
232 // End reconnecting after a specific timeout and flush all commands with a individual error
233 return new Error('Retry time exhausted');
234 }
235 if (options.times_connected > 10) {
236 // End reconnecting with built in error
237 return undefined;
238 }
239 // reconnect after
240 return Math.max(options.attempt * 100, 3000);
241 }
242});
243```
244
245## client.auth(password[, callback])
246
247When connecting to a Redis server that requires authentication, the `AUTH` command must be sent as the
248first command after connecting. This can be tricky to coordinate with reconnections, the ready check,
249etc. To make this easier, `client.auth()` stashes `password` and will send it after each connection,
250including reconnections. `callback` is invoked only once, after the response to the very first
251`AUTH` command sent.
252NOTE: Your call to `client.auth()` should not be inside the ready handler. If
253you are doing this wrong, `client` will emit an error that looks
254something like this `Error: Ready check failed: ERR operation not permitted`.
255
256## backpressure
257
258### stream
259
260The client exposed the used [stream](https://nodejs.org/api/stream.html) in `client.stream` and if the stream or client had to [buffer](https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback) the command in `client.should_buffer`.
261In combination this can be used to implement backpressure by checking the buffer state before sending a command and listening to the stream [drain](https://nodejs.org/api/stream.html#stream_event_drain) event.
262
263## client.quit()
264
265This sends the quit command to the redis server and ends cleanly right after all running commands were properly handled.
266If this is called while reconnecting (and therefor no connection to the redis server exists) it is going to end the connection right away instead of
267resulting in further reconnections! All offline commands are going to be flushed with an error in that case.
268
269## client.end(flush)
270
271Forcibly close the connection to the Redis server. Note that this does not wait until all replies have been parsed.
272If you want to exit cleanly, call `client.quit()` as mentioned above.
273
274You should set flush to true, if you are not absolutely sure you do not care about any other commands.
275If you set flush to false all still running commands will silently fail.
276
277This example closes the connection to the Redis server before the replies have been read. You probably don't
278want to do this:
279
280```js
281var redis = require("redis"),
282 client = redis.createClient();
283
284client.set("foo_rand000000000000", "some fantastic value", function (err, reply) {
285 // This will either result in an error (flush parameter is set to true)
286 // or will silently fail and this callback will not be called at all (flush set to false)
287 console.log(err);
288});
289client.end(true); // No further commands will be processed
290client.get("foo_rand000000000000", function (err, reply) {
291 console.log(err); // => 'The connection has already been closed.'
292});
293```
294
295`client.end()` without the flush parameter set to true should NOT be used in production!
296
297## Error handling (>= v.2.6)
298
299All redis errors are returned as `ReplyError`.
300All unresolved commands that get rejected due to what ever reason return a `AbortError`.
301As subclass of the `AbortError` a `AggregateError` exists. This is emitted in case multiple unresolved commands without callback got rejected in debug_mode.
302They are all aggregated and a single error is emitted in that case.
303
304Example:
305```js
306var redis = require('./');
307var assert = require('assert');
308var client = redis.createClient();
309
310client.on('error', function (err) {
311 assert(err instanceof Error);
312 assert(err instanceof redis.AbortError);
313 assert(err instanceof redis.AggregateError);
314 assert.strictEqual(err.errors.length, 2); // The set and get got aggregated in here
315 assert.strictEqual(err.code, 'NR_CLOSED');
316});
317client.set('foo', 123, 'bar', function (err, res) { // To many arguments
318 assert(err instanceof redis.ReplyError); // => true
319 assert.strictEqual(err.command, 'SET');
320 assert.deepStrictEqual(err.args, ['foo', 123, 'bar']);
321
322 redis.debug_mode = true;
323 client.set('foo', 'bar');
324 client.get('foo');
325 process.nextTick(function () {
326 client.end(true); // Force closing the connection while the command did not yet return
327 redis.debug_mode = false;
328 });
329});
330
331```
332
333Every `ReplyError` contains the `command` name in all-caps and the arguments (`args`).
334
335If node_redis emits a library error because of another error, the triggering error is added to the returned error as `origin` attribute.
336
337___Error codes___
338
339node_redis returns a `NR_CLOSED` error code if the clients connection dropped. If a command unresolved command got rejected a `UNERCTAIN_STATE` code is returned.
340A `CONNECTION_BROKEN` error code is used in case node_redis gives up to reconnect.
341
342## client.unref()
343
344Call `unref()` on the underlying socket connection to the Redis server, allowing the program to exit once no more commands are pending.
345
346This is an **experimental** feature, and only supports a subset of the Redis protocol. Any commands where client state is saved on the Redis server, e.g. `*SUBSCRIBE` or the blocking `BL*` commands will *NOT* work with `.unref()`.
347
348```js
349var redis = require("redis")
350var client = redis.createClient()
351
352/*
353 Calling unref() will allow this program to exit immediately after the get command finishes. Otherwise the client would hang as long as the client-server connection is alive.
354*/
355client.unref()
356client.get("foo", function (err, value){
357 if (err) throw(err)
358 console.log(value)
359})
360```
361
362## Friendlier hash commands
363
364Most Redis commands take a single String or an Array of Strings as arguments, and replies are sent back as a single String or an Array of Strings.
365When dealing with hash values, there are a couple of useful exceptions to this.
366
367### client.hgetall(hash, callback)
368
369The reply from an HGETALL command will be converted into a JavaScript Object by `node_redis`. That way you can interact
370with the responses using JavaScript syntax.
371
372Example:
373
374```js
375client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234");
376client.hgetall("hosts", function (err, obj) {
377 console.dir(obj);
378});
379```
380
381Output:
382
383```js
384{ mjr: '1', another: '23', home: '1234' }
385```
386
387### client.hmset(hash, obj[, callback])
388
389Multiple values in a hash can be set by supplying an object:
390
391```js
392client.HMSET(key2, {
393 "0123456789": "abcdefghij", // NOTE: key and value will be coerced to strings
394 "some manner of key": "a type of value"
395});
396```
397
398The properties and values of this Object will be set as keys and values in the Redis hash.
399
400### client.hmset(hash, key1, val1, ... keyn, valn, [callback])
401
402Multiple values may also be set by supplying a list:
403
404```js
405client.HMSET(key1, "0123456789", "abcdefghij", "some manner of key", "a type of value");
406```
407
408## Publish / Subscribe
409
410Example of the publish / subscribe API. This program opens two
411client connections, subscribes to a channel on one of them, and publishes to that
412channel on the other:
413
414```js
415var redis = require("redis");
416var sub = redis.createClient(), pub = redis.createClient();
417var msg_count = 0;
418
419sub.on("subscribe", function (channel, count) {
420 pub.publish("a nice channel", "I am sending a message.");
421 pub.publish("a nice channel", "I am sending a second message.");
422 pub.publish("a nice channel", "I am sending my last message.");
423});
424
425sub.on("message", function (channel, message) {
426 console.log("sub channel " + channel + ": " + message);
427 msg_count += 1;
428 if (msg_count === 3) {
429 sub.unsubscribe();
430 sub.quit();
431 pub.quit();
432 }
433});
434
435sub.subscribe("a nice channel");
436```
437
438When a client issues a `SUBSCRIBE` or `PSUBSCRIBE`, that connection is put into a "subscriber" mode.
439At that point, only commands that modify the subscription set are valid and quit (and depending on the redis version ping as well). When the subscription
440set is empty, the connection is put back into regular mode.
441
442If you need to send regular commands to Redis while in subscriber mode, just open another connection with a new client (hint: use `client.duplicate()`).
443
444## Subscriber Events
445
446If a client has subscriptions active, it may emit these events:
447
448### "message" (channel, message)
449
450Client will emit `message` for every message received that matches an active subscription.
451Listeners are passed the channel name as `channel` and the message as `message`.
452
453### "pmessage" (pattern, channel, message)
454
455Client will emit `pmessage` for every message received that matches an active subscription pattern.
456Listeners are passed the original pattern used with `PSUBSCRIBE` as `pattern`, the sending channel
457name as `channel`, and the message as `message`.
458
459### "message_buffer" (channel, message)
460
461This is the same as the `message` event with the exception, that it is always going to emit a buffer.
462If you listen to the `message` event at the same time as the `message_buffer`, it is always going to emit a string.
463
464### "pmessage_buffer" (pattern, channel, message)
465
466This is the same as the `pmessage` event with the exception, that it is always going to emit a buffer.
467If you listen to the `pmessage` event at the same time as the `pmessage_buffer`, it is always going to emit a string.
468
469### "subscribe" (channel, count)
470
471Client will emit `subscribe` in response to a `SUBSCRIBE` command. Listeners are passed the
472channel name as `channel` and the new count of subscriptions for this client as `count`.
473
474### "psubscribe" (pattern, count)
475
476Client will emit `psubscribe` in response to a `PSUBSCRIBE` command. Listeners are passed the
477original pattern as `pattern`, and the new count of subscriptions for this client as `count`.
478
479### "unsubscribe" (channel, count)
480
481Client will emit `unsubscribe` in response to a `UNSUBSCRIBE` command. Listeners are passed the
482channel name as `channel` and the new count of subscriptions for this client as `count`. When
483`count` is 0, this client has left subscriber mode and no more subscriber events will be emitted.
484
485### "punsubscribe" (pattern, count)
486
487Client will emit `punsubscribe` in response to a `PUNSUBSCRIBE` command. Listeners are passed the
488channel name as `channel` and the new count of subscriptions for this client as `count`. When
489`count` is 0, this client has left subscriber mode and no more subscriber events will be emitted.
490
491## client.multi([commands])
492
493`MULTI` commands are queued up until an `EXEC` is issued, and then all commands are run atomically by
494Redis. The interface in `node_redis` is to return an individual `Multi` object by calling `client.multi()`.
495If any command fails to queue, all commands are rolled back and none is going to be executed (For further information look at [transactions](http://redis.io/topics/transactions)).
496
497```js
498var redis = require("./index"),
499 client = redis.createClient(), set_size = 20;
500
501client.sadd("bigset", "a member");
502client.sadd("bigset", "another member");
503
504while (set_size > 0) {
505 client.sadd("bigset", "member " + set_size);
506 set_size -= 1;
507}
508
509// multi chain with an individual callback
510client.multi()
511 .scard("bigset")
512 .smembers("bigset")
513 .keys("*", function (err, replies) {
514 // NOTE: code in this callback is NOT atomic
515 // this only happens after the the .exec call finishes.
516 client.mget(replies, redis.print);
517 })
518 .dbsize()
519 .exec(function (err, replies) {
520 console.log("MULTI got " + replies.length + " replies");
521 replies.forEach(function (reply, index) {
522 console.log("Reply " + index + ": " + reply.toString());
523 });
524 });
525```
526
527### Multi.exec([callback])
528
529`client.multi()` is a constructor that returns a `Multi` object. `Multi` objects share all of the
530same command methods as `client` objects do. Commands are queued up inside the `Multi` object
531until `Multi.exec()` is invoked.
532
533If your code contains an syntax error an EXECABORT error is going to be thrown and all commands are going to be aborted. That error contains a `.errors` property that contains the concret errors.
534If all commands were queued successfully and an error is thrown by redis while processing the commands that error is going to be returned in the result array! No other command is going to be aborted though than the onces failing.
535
536You can either chain together `MULTI` commands as in the above example, or you can queue individual
537commands while still sending regular client command as in this example:
538
539```js
540var redis = require("redis"),
541 client = redis.createClient(), multi;
542
543// start a separate multi command queue
544multi = client.multi();
545multi.incr("incr thing", redis.print);
546multi.incr("incr other thing", redis.print);
547
548// runs immediately
549client.mset("incr thing", 100, "incr other thing", 1, redis.print);
550
551// drains multi queue and runs atomically
552multi.exec(function (err, replies) {
553 console.log(replies); // 101, 2
554});
555```
556
557In addition to adding commands to the `MULTI` queue individually, you can also pass an array
558of commands and arguments to the constructor:
559
560```js
561var redis = require("redis"),
562 client = redis.createClient(), multi;
563
564client.multi([
565 ["mget", "multifoo", "multibar", redis.print],
566 ["incr", "multifoo"],
567 ["incr", "multibar"]
568]).exec(function (err, replies) {
569 console.log(replies);
570});
571```
572
573### Multi.exec_atomic([callback])
574
575Identical to Multi.exec but with the difference that executing a single command will not use transactions.
576
577## client.batch([commands])
578
579Identical to .multi without transactions. This is recommended if you want to execute many commands at once but don't have to rely on transactions.
580
581`BATCH` commands are queued up until an `EXEC` is issued, and then all commands are run atomically by
582Redis. The interface in `node_redis` is to return an individual `Batch` object by calling `client.batch()`.
583The only difference between .batch and .multi is that no transaction is going to be used.
584Be aware that the errors are - just like in multi statements - in the result. Otherwise both, errors and results could be returned at the same time.
585
586If you fire many commands at once this is going to boost the execution speed significantly compared to fireing the same commands in a loop without waiting for the result! See the benchmarks for further comparison. Please remember that all commands are kept in memory until they are fired.
587
588## Monitor mode
589
590Redis supports the `MONITOR` command, which lets you see all commands received by the Redis server
591across all client connections, including from other client libraries and other computers.
592
593A `monitor` event is going to be emitted for every command fired from any client connected to the server including the monitoring client itself.
594The callback for the `monitor` event takes a timestamp from the Redis server, an array of command arguments and the raw monitoring string.
595
596Example:
597
598```js
599var client = require("redis").createClient();
600client.monitor(function (err, res) {
601 console.log("Entering monitoring mode.");
602});
603client.set('foo', 'bar');
604
605client.on("monitor", function (time, args, raw_reply) {
606 console.log(time + ": " + args); // 1458910076.446514:['set', 'foo', 'bar']
607});
608```
609
610# Extras
611
612Some other things you might like to know about.
613
614## client.server_info
615
616After the ready probe completes, the results from the INFO command are saved in the `client.server_info`
617object.
618
619The `versions` key contains an array of the elements of the version string for easy comparison.
620
621 > client.server_info.redis_version
622 '2.3.0'
623 > client.server_info.versions
624 [ 2, 3, 0 ]
625
626## redis.print()
627
628A handy callback function for displaying return values when testing. Example:
629
630```js
631var redis = require("redis"),
632 client = redis.createClient();
633
634client.on("connect", function () {
635 client.set("foo_rand000000000000", "some fantastic value", redis.print);
636 client.get("foo_rand000000000000", redis.print);
637});
638```
639
640This will print:
641
642 Reply: OK
643 Reply: some fantastic value
644
645Note that this program will not exit cleanly because the client is still connected.
646
647## Multi-word commands
648
649To execute redis multi-word commands like `SCRIPT LOAD` or `CLIENT LIST` pass
650the second word as first parameter:
651
652 client.script('load', 'return 1');
653 client.multi().script('load', 'return 1').exec(...);
654 client.multi([['script', 'load', 'return 1']]).exec(...);
655
656## client.duplicate([options][, callback])
657
658Duplicate all current options and return a new redisClient instance. All options passed to the duplicate function are going to replace the original option.
659If you pass a callback, duplicate is going to wait until the client is ready and returns it in the callback. If an error occurs in the meanwhile, that is going to return an error instead in the callback.
660
661One example of when to use duplicate() would be to accomodate the connection-
662blocking redis commands BRPOP, BLPOP, and BRPOPLPUSH. If these commands
663are used on the same redisClient instance as non-blocking commands, the
664non-blocking ones may be queued up until after the blocking ones finish.
665
666 var Redis=require('redis');
667 var client = Redis.createClient();
668 var clientBlocking = client.duplicate();
669
670 var get = function() {
671 console.log("get called");
672 client.get("any_key",function() { console.log("get returned"); });
673 setTimeout( get, 1000 );
674 };
675 var brpop = function() {
676 console.log("brpop called");
677 clientBlocking.brpop("nonexistent", 5, function() {
678 console.log("brpop return");
679 setTimeout( brpop, 1000 );
680 });
681 };
682 get();
683 brpop();
684
685Another reason to use duplicate() is when multiple DBs on the same server are
686accessed via the redis SELECT command. Each DB could use its own connection.
687
688## client.send_command(command_name[, [args][, callback]])
689
690All Redis commands have been added to the `client` object. However, if new commands are introduced before this library is updated,
691you can use `send_command()` to send arbitrary commands to Redis.
692The command_name has to be lower case.
693
694All commands are sent as multi-bulk commands. `args` can either be an Array of arguments, or omitted / set to undefined.
695
696## client.connected
697
698Boolean tracking the state of the connection to the Redis server.
699
700## client.command_queue_length
701
702The number of commands that have been sent to the Redis server but not yet replied to. You can use this to
703enforce some kind of maximum queue depth for commands while connected.
704
705## client.offline_queue_length
706
707The number of commands that have been queued up for a future connection. You can use this to enforce
708some kind of maximum queue depth for pre-connection commands.
709
710### Commands with Optional and Keyword arguments
711
712This applies to anything that uses an optional `[WITHSCORES]` or `[LIMIT offset count]` in the [redis.io/commands](http://redis.io/commands) documentation.
713
714Example:
715```js
716var args = [ 'myzset', 1, 'one', 2, 'two', 3, 'three', 99, 'ninety-nine' ];
717client.zadd(args, function (err, response) {
718 if (err) throw err;
719 console.log('added '+response+' items.');
720
721 // -Infinity and +Infinity also work
722 var args1 = [ 'myzset', '+inf', '-inf' ];
723 client.zrevrangebyscore(args1, function (err, response) {
724 if (err) throw err;
725 console.log('example1', response);
726 // write your code here
727 });
728
729 var max = 3, min = 1, offset = 1, count = 2;
730 var args2 = [ 'myzset', max, min, 'WITHSCORES', 'LIMIT', offset, count ];
731 client.zrevrangebyscore(args2, function (err, response) {
732 if (err) throw err;
733 console.log('example2', response);
734 // write your code here
735 });
736});
737```
738
739## Performance
740
741Much effort has been spent to make `node_redis` as fast as possible for common
742operations.
743
744```
745Lenovo T450s, i7-5600U and 12gb memory
746clients: 1, NodeJS: 6.2.0, Redis: 3.2.0, parser: javascript, connected by: tcp
747 PING, 1/1 avg/max: 0.02/ 5.26 2501ms total, 46916 ops/sec
748 PING, batch 50/1 avg/max: 0.06/ 4.35 2501ms total, 755178 ops/sec
749 SET 4B str, 1/1 avg/max: 0.02/ 4.75 2501ms total, 40856 ops/sec
750 SET 4B str, batch 50/1 avg/max: 0.11/ 1.51 2501ms total, 432727 ops/sec
751 SET 4B buf, 1/1 avg/max: 0.05/ 2.76 2501ms total, 20659 ops/sec
752 SET 4B buf, batch 50/1 avg/max: 0.25/ 1.76 2501ms total, 194962 ops/sec
753 GET 4B str, 1/1 avg/max: 0.02/ 1.55 2501ms total, 45156 ops/sec
754 GET 4B str, batch 50/1 avg/max: 0.09/ 3.15 2501ms total, 524110 ops/sec
755 GET 4B buf, 1/1 avg/max: 0.02/ 3.07 2501ms total, 44563 ops/sec
756 GET 4B buf, batch 50/1 avg/max: 0.10/ 3.18 2501ms total, 473171 ops/sec
757 SET 4KiB str, 1/1 avg/max: 0.03/ 1.54 2501ms total, 32627 ops/sec
758 SET 4KiB str, batch 50/1 avg/max: 0.34/ 1.89 2501ms total, 146861 ops/sec
759 SET 4KiB buf, 1/1 avg/max: 0.05/ 2.85 2501ms total, 20688 ops/sec
760 SET 4KiB buf, batch 50/1 avg/max: 0.36/ 1.83 2501ms total, 138165 ops/sec
761 GET 4KiB str, 1/1 avg/max: 0.02/ 1.37 2501ms total, 39389 ops/sec
762 GET 4KiB str, batch 50/1 avg/max: 0.24/ 1.81 2501ms total, 208157 ops/sec
763 GET 4KiB buf, 1/1 avg/max: 0.02/ 2.63 2501ms total, 39918 ops/sec
764 GET 4KiB buf, batch 50/1 avg/max: 0.31/ 8.56 2501ms total, 161575 ops/sec
765 INCR, 1/1 avg/max: 0.02/ 4.69 2501ms total, 45685 ops/sec
766 INCR, batch 50/1 avg/max: 0.09/ 3.06 2501ms total, 539964 ops/sec
767 LPUSH, 1/1 avg/max: 0.02/ 3.04 2501ms total, 41253 ops/sec
768 LPUSH, batch 50/1 avg/max: 0.12/ 1.94 2501ms total, 425090 ops/sec
769 LRANGE 10, 1/1 avg/max: 0.02/ 2.28 2501ms total, 39850 ops/sec
770 LRANGE 10, batch 50/1 avg/max: 0.25/ 1.85 2501ms total, 194302 ops/sec
771 LRANGE 100, 1/1 avg/max: 0.05/ 2.93 2501ms total, 21026 ops/sec
772 LRANGE 100, batch 50/1 avg/max: 1.52/ 2.89 2501ms total, 32767 ops/sec
773 SET 4MiB str, 1/1 avg/max: 5.16/ 15.55 2502ms total, 193 ops/sec
774 SET 4MiB str, batch 20/1 avg/max: 89.73/ 99.96 2513ms total, 223 ops/sec
775 SET 4MiB buf, 1/1 avg/max: 2.23/ 8.35 2501ms total, 446 ops/sec
776 SET 4MiB buf, batch 20/1 avg/max: 41.47/ 50.91 2530ms total, 482 ops/sec
777 GET 4MiB str, 1/1 avg/max: 2.79/ 10.91 2502ms total, 358 ops/sec
778 GET 4MiB str, batch 20/1 avg/max: 101.61/118.11 2541ms total, 197 ops/sec
779 GET 4MiB buf, 1/1 avg/max: 2.32/ 14.93 2502ms total, 430 ops/sec
780 GET 4MiB buf, batch 20/1 avg/max: 65.01/ 84.72 2536ms total, 308 ops/sec
781 ```
782
783## Debugging
784
785To get debug output run your `node_redis` application with `NODE_DEBUG=redis`.
786
787This is also going to result in good stack traces opposed to useless ones otherwise for any async operation.
788If you only want to have good stack traces but not the debug output run your application in development mode instead (`NODE_ENV=development`).
789
790Good stack traces are only activated in development and debug mode as this results in a significant performance penalty.
791
792___Comparison___:
793Useless stack trace:
794```
795ReplyError: ERR wrong number of arguments for 'set' command
796 at parseError (/home/ruben/repos/redis/node_modules/redis-parser/lib/parser.js:158:12)
797 at parseType (/home/ruben/repos/redis/node_modules/redis-parser/lib/parser.js:219:14)
798```
799Good stack trace:
800```
801ReplyError: ERR wrong number of arguments for 'set' command
802 at new Command (/home/ruben/repos/redis/lib/command.js:9:902)
803 at RedisClient.set (/home/ruben/repos/redis/lib/commands.js:9:3238)
804 at Context.<anonymous> (/home/ruben/repos/redis/test/good_stacks.spec.js:20:20)
805 at callFnAsync (/home/ruben/repos/redis/node_modules/mocha/lib/runnable.js:349:8)
806 at Test.Runnable.run (/home/ruben/repos/redis/node_modules/mocha/lib/runnable.js:301:7)
807 at Runner.runTest (/home/ruben/repos/redis/node_modules/mocha/lib/runner.js:422:10)
808 at /home/ruben/repos/redis/node_modules/mocha/lib/runner.js:528:12
809 at next (/home/ruben/repos/redis/node_modules/mocha/lib/runner.js:342:14)
810 at /home/ruben/repos/redis/node_modules/mocha/lib/runner.js:352:7
811 at next (/home/ruben/repos/redis/node_modules/mocha/lib/runner.js:284:14)
812 at Immediate._onImmediate (/home/ruben/repos/redis/node_modules/mocha/lib/runner.js:320:5)
813 at processImmediate [as _immediateCallback] (timers.js:383:17)
814```
815
816## How to Contribute
817- Open a pull request or an issue about what you want to implement / change. We're glad for any help!
818 - Please be aware that we'll only accept fully tested code.
819
820## Contributors
821
822The original author of node_redis is [Matthew Ranney](https://github.com/mranney)
823
824The current lead maintainer is [Ruben Bridgewater](https://github.com/BridgeAR)
825
826Many [others](https://github.com/NodeRedis/node_redis/graphs/contributors) contributed to `node_redis` too. Thanks to all of them!
827
828## License
829
830[MIT](LICENSE)
831
832### Consolidation: It's time for celebration
833
834Right now there are two great redis clients around and both have some advantages above each other. We speak about ioredis and node_redis. So after talking to each other about how we could improve in working together we (that is @luin and @BridgeAR) decided to work towards a single library on the long run. But step by step.
835
836First of all, we want to split small parts of our libraries into others so that we're both able to use the same code. Those libraries are going to be maintained under the NodeRedis organization. This is going to reduce the maintance overhead, allows others to use the very same code, if they need it and it's way easyer for others to contribute to both libraries.
837
838We're very happy about this step towards working together as we both want to give you the best redis experience possible.
839
840If you want to join our cause by help maintaining something, please don't hesitate to contact either one of us.
841
\No newline at end of file