The performance of node_redis improves dramatically with pipelining, which happens automatically in most normal programs.

Sending Commands

Each Redis command is exposed as a function on the client object.
All functions take either an args Array plus optional callback Function or
a variable number of individual arguments followed by an optional callback.
Here is an example of passing an array of arguments and a callback:

API

Connection Events

client will emit some events about the state of the connection to the Redis server.

"ready"

client will emit ready a connection is established to the Redis server and the server reports
that it is ready to receive commands. Commands issued before the ready event are queued,
then replayed just before this event is emitted.

"connect"

client will emit connect at the same time as it emits ready unless client.options.no_ready_check
is set. If this options is set, connect will be emitted when the stream is connected, and then
you are free to try to send commands.

"error"

client will emit error when encountering an error connecting to the Redis server.

Note that "error" is a special event type in node. If there are no listeners for an
"error" event, node will exit. This is usually what you want, but it can lead to some
cryptic error messages like this:

Not very useful in diagnosing the problem, but if your program isn't ready to handle this,
it is probably the right thing to just exit.

client will also emit error if an exception is thrown inside of node_redis for whatever reason.
It would be nice to distinguish these two cases.

"end"

client will emit end when an established Redis server connection has closed.

"drain"

client will emit drain when the TCP connection to the Redis server has been buffering, but is now
writable. This event can be used to stream commands in to Redis and adapt to backpressure. Right now,
you need to check client.command_queue.length to decide when to reduce your send rate. Then you can
resume sending when you get drain.

"idle"

client will emit idle when there are no outstanding commands that are awaiting a response.

redis.createClient(port, host, options)

Create a new client connection. port defaults to 6379 and host defaults
to 127.0.0.1. If you have redis-server running on the same computer as node, then the defaults for
port and host are probably fine. options in an object with the following possible properties:

parser: which Redis protocol reply parser to use. Defaults to hiredis if that module is installed.
This may also be set to javascript.

return_buffers: defaults to false. If set to true, then all replies will be sent to callbacks as node Buffer
objects instead of JavaScript Strings.

detect_buffers: default to false. If set to true, then replies will be sent to callbacks as node Buffer objects
if any of the input arguments to the original command were Buffer objects.
This option lets you switch between Buffers and Strings on a per-command basis, whereas return_buffers applies to
every command on a client.

socket_nodelay: defaults to true. Whether to call setNoDelay() on the TCP stream, which disables the
Nagle algorithm on the underlying socket. Setting this option to false can result in additional throughput at the
cost of more latency. Most applications will want this set to true.

no_ready_check: defaults to false. When a connection is established to the Redis server, the server might still
be loading the database from disk. While loading, the server 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.

enable_offline_queue: defaults to 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 execute immediately
with an error, or an error will be thrown if no callback is specified.

createClient() returns a RedisClient object that is named client in all of the examples here.

client.auth(password, callback)

When connecting to Redis servers that require authentication, the AUTH command must be sent as the
first command after connecting. This can be tricky to coordinate with reconnections, the ready check,
etc. To make this easier, client.auth() stashes password and will send it after each connection,
including reconnections. callback is invoked only once, after the response to the very first
AUTH command sent.
NOTE: Your call to client.auth() should not be inside the ready handler. If
you are doing this wrong, client will emit an error that looks
something like this Error: Ready check failed: ERR operation not permitted.

client.end()

Forcibly close the connection to the Redis server. Note that this does not wait until all replies have been parsed.
If you want to exit cleanly, call client.quit() to send the QUIT command after you have handled all replies.

This example closes the connection to the Redis server before the replies have been read. You probably don't
want to do this:

client.end() is useful for timeout cases where something is stuck or taking too long and you want
to start over.

Friendlier hash commands

Most 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.
When dealing with hash values, there are a couple of useful exceptions to this.

client.hgetall(hash)

The reply from an HGETALL command will be converted into a JavaScript Object by node_redis. That way you can interact
with the responses using JavaScript syntax.

When a client issues a SUBSCRIBE or PSUBSCRIBE, that connection is put into "pub/sub" mode.
At that point, only commands that modify the subscription set are valid. When the subscription
set is empty, the connection is put back into regular mode.

If you need to send regular commands to Redis while in pub/sub mode, just open another connection.

Pub / Sub Events

If a client has subscriptions active, it may emit these events:

"message" (channel, message)

Client will emit message for every message received that matches an active subscription.
Listeners are passed the channel name as channel and the message Buffer as message.

"pmessage" (pattern, channel, message)

Client will emit pmessage for every message received that matches an active subscription pattern.
Listeners are passed the original pattern used with PSUBSCRIBE as pattern, the sending channel
name as channel, and the message Buffer as message.

"subscribe" (channel, count)

Client will emit subscribe in response to a SUBSCRIBE command. Listeners are passed the
channel name as channel and the new count of subscriptions for this client as count.

"psubscribe" (pattern, count)

Client will emit psubscribe in response to a PSUBSCRIBE command. Listeners are passed the
original pattern as pattern, and the new count of subscriptions for this client as count.

"unsubscribe" (channel, count)

Client will emit unsubscribe in response to a UNSUBSCRIBE command. Listeners are passed the
channel name as channel and the new count of subscriptions for this client as count. When
count is 0, this client has left pub/sub mode and no more pub/sub events will be emitted.

"punsubscribe" (pattern, count)

Client will emit punsubscribe in response to a PUNSUBSCRIBE command. Listeners are passed the
channel name as channel and the new count of subscriptions for this client as count. When
count is 0, this client has left pub/sub mode and no more pub/sub events will be emitted.

client.multi([commands])

MULTI commands are queued up until an EXEC is issued, and then all commands are run atomically by
Redis. The interface in node_redis is to return an individual Multi object by calling client.multi().

client.multi() is a constructor that returns a Multi object. Multi objects share all of the
same command methods as client objects do. Commands are queued up inside the Multi object
until Multi.exec() is invoked.

You can either chain together MULTI commands as in the above example, or you can queue individual
commands while still sending regular client command as in this example:

Monitor mode

Redis supports the MONITOR command, which lets you see all commands received by the Redis server
across all client connections, including from other client libraries and other computers.

After you send the MONITOR command, no other commands are valid on that connection. node_redis
will emit a monitor event for every new monitor message that comes across. The callback for the
monitor event takes a timestamp from the Redis server and an array of command arguments.

send command is data sent into Redis and on_data is data received from Redis.

client.send_command(command_name, args, callback)

Used internally to send commands to Redis. For convenience, nearly all commands that are published on the Redis
Wiki have been added to the client object. However, if I missed any, or if new commands are introduced before
this library is updated, you can use send_command() to send arbitrary commands to Redis.

All commands are sent as multi-bulk commands. args can either be an Array of arguments, or omitted.

client.connected

Boolean tracking the state of the connection to the Redis server.

client.command_queue.length

The number of commands that have been sent to the Redis server but not yet replied to. You can use this to
enforce some kind of maximum queue depth for commands while connected.

Don't mess with client.command_queue though unless you really know what you are doing.

client.offline_queue.length

The number of commands that have been queued up for a future connection. You can use this to enforce
some kind of maximum queue depth for pre-connection commands.

client.retry_delay

Current delay in milliseconds before a connection retry will be attempted. This starts at 250.

client.retry_backoff

Multiplier for future retry timeouts. This should be larger than 1 to add more time between retries.
Defaults to 1.7. The default initial connection retry is 250, so the second retry will be 425, followed by 723.5, etc.

Commands with Optional and Keyword arguments

This applies to anything that uses an optional [WITHSCORES] or [LIMIT offset count] in the redis.io/commands documentation.

LICENSE - "MIT License"

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.