redis
A modern, high performance Redis client
Last updated a year ago by leibale .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install redis 
SYNC missed versions from official npm registry.

Node-Redis

Tests Coverage License

Discord Twitch YouTube Twitter

node-redis is a modern, high performance Redis client for Node.js.

Installation

Start a redis via docker:

docker run -p 6379:6379 -it redis/redis-stack-server:latest

To install node-redis, simply:

npm install redis

Looking for a high-level library to handle object mapping? See redis-om-node!

Usage

Basic Example

import { createClient } from 'redis';

const client = await createClient()
  .on('error', err => console.log('Redis Client Error', err))
  .connect();

await client.set('key', 'value');
const value = await client.get('key');
await client.close();

:warning: You MUST listen to error events. If a client doesn't have at least one error listener registered and an error occurs, that error will be thrown and the Node.js process will exit. See the EventEmitter docs for more details.

The above code connects to localhost on port 6379. To connect to a different host or port, use a connection string in the format redis[s]://[[username][:password]@][host][:port][/db-number]:

createClient({
  url: 'redis://alice:foobared@awesome.redis.server:6380'
});

You can also use discrete parameters, UNIX sockets, and even TLS to connect. Details can be found in the client configuration guide.

To check if the the client is connected and ready to send commands, use client.isReady which returns a boolean. client.isOpen is also available. This returns true when the client's underlying socket is open, and false when it isn't (for example when the client is still connecting or reconnecting after a network error).

Redis Commands

There is built-in support for all of the out-of-the-box Redis commands. They are exposed using the raw Redis command names (HSET, HGETALL, etc.) and a friendlier camel-cased version (hSet, hGetAll, etc.):

// raw Redis commands
await client.HSET('key', 'field', 'value');
await client.HGETALL('key');

// friendly JavaScript commands
await client.hSet('key', 'field', 'value');
await client.hGetAll('key');

Modifiers to commands are specified using a JavaScript object:

await client.set('key', 'value', {
  expiration: {
    type: 'EX',
    value: 10
  },
  condition: 'NX'
});

Replies will be mapped to useful data structures:

await client.hGetAll('key'); // { field1: 'value1', field2: 'value2' }
await client.hVals('key'); // ['value1', 'value2']

NOTE: you can change the default type mapping. See the Type Mapping documentation for more information.

Unsupported Redis Commands

If you want to run commands and/or use arguments that Node Redis doesn't know about (yet!) use .sendCommand():

await client.sendCommand(['SET', 'key', 'value', 'NX']); // 'OK'

await client.sendCommand(['HGETALL', 'key']); // ['key1', 'field1', 'key2', 'field2']

Disconnecting

There are two functions that disconnect a client from the Redis server. In most scenarios you should use .close() to ensure that pending commands are sent to Redis before closing a connection.

:warning: The .quit() and .disconnect() methods have been deprecated in v5. For more details, refer to the v4-to-v5 guide.

.close()

const [ping, get] = await Promise.all([
  client.ping(),
  client.get('key'),
  client.close()
]); // ['PONG', null]

try {
  await client.get('key');
} catch (err) {
  // ClientClosedError
}

:warning: .close is just like .quit() which was depreacted in Redis 7.2. See the relevant section in the migration guide for more information.

.destroy()

Forcibly close a client's connection to Redis immediately. Calling destroy will not send further pending commands to the Redis server, or wait for or parse outstanding responses.

Auto-Pipelining

Node Redis will automatically pipeline requests that are made during the same "tick".

client.set('Tm9kZSBSZWRpcw==', 'users:1');
client.sAdd('users:1:tokens', 'Tm9kZSBSZWRpcw==');

Of course, if you don't do something with your Promises you're certain to get unhandled Promise exceptions. To take advantage of auto-pipelining and handle your Promises, use Promise.all().

await Promise.all([
  client.set('Tm9kZSBSZWRpcw==', 'users:1'),
  client.sAdd('users:1:tokens', 'Tm9kZSBSZWRpcw==')
]);

Events

The Node Redis client class is an Nodejs EventEmitter and it emits an event each time the network status changes:

Name When Listener arguments
connect Initiating a connection to the server No arguments
ready Client is ready to use No arguments
end Connection has been closed (via .quit() or .disconnect()) No arguments
error An error has occurred—usually a network issue such as "Socket closed unexpectedly" (error: Error)
reconnecting Client is trying to reconnect to the server No arguments
sharded-channel-moved See here See here

:warning: You MUST listen to error events. If a client doesn't have at least one error listener registered and an error occurs, that error will be thrown and the Node.js process will exit. See the EventEmitter docs for more details.

The client will not emit any other events beyond those listed above.

Links

Supported Redis versions

Node Redis is supported with the following versions of Redis:

Version Supported
7.0.z :heavy_check_mark:
6.2.z :heavy_check_mark:
6.0.z :heavy_check_mark:
5.0.z :heavy_check_mark:
< 5.0 :x:

Node Redis should work with older versions of Redis, but it is not fully tested and we cannot offer support.

Contributing

If you'd like to contribute, check out the contributing guide.

Thank you to all the people who already contributed to Node Redis!

Contributors

License

This repository is licensed under the "MIT" license. See LICENSE.

Current Tags

  • 4.7.0                                ...           latest (5 months ago)
  • 5.0.0-next.5                                ...           next (2 months ago)

156 Versions

  • 5.0.0-next.5                                ...           2 months ago
  • 4.7.0                                ...           5 months ago
  • 4.6.15                                ...           6 months ago
  • 4.6.14                                ...           7 months ago
  • 4.6.13                                ...           a year ago
  • 4.6.12                                ...           a year ago
  • 4.6.11                                ...           a year ago
  • 5.0.0-next.4                                ...           a year ago
  • 5.0.0-next.3                                ...           a year ago
  • 5.0.0-next.2                                ...           a year ago
  • 5.0.0-next.1                                ...           a year ago
  • 4.6.10                                ...           a year ago
  • 4.6.9                                ...           a year ago
  • 4.6.8                                ...           a year ago
  • 4.6.7                                ...           2 years ago
  • 4.6.6                                ...           2 years ago
  • 4.6.5                                ...           2 years ago
  • 4.6.4                                ...           2 years ago
  • 4.6.3                                ...           2 years ago
  • 4.6.2                                ...           2 years ago
  • 4.6.1                                ...           2 years ago
  • 4.6.0                                ...           2 years ago
  • 4.5.1                                ...           2 years ago
  • 4.5.0                                ...           2 years ago
  • 4.4.0                                ...           2 years ago
  • 4.3.1                                ...           2 years ago
  • 4.3.0                                ...           2 years ago
  • 4.2.0                                ...           2 years ago
  • 4.1.1                                ...           2 years ago
  • 4.1.0                                ...           3 years ago
  • 4.0.6                                ...           3 years ago
  • 4.0.5 [deprecated]           ...           3 years ago
  • 4.0.4                                ...           3 years ago
  • 4.0.3                                ...           3 years ago
  • 4.0.2                                ...           3 years ago
  • 4.0.1                                ...           3 years ago
  • 4.0.0                                ...           3 years ago
  • 4.0.0-rc.4                                ...           3 years ago
  • 4.0.0-rc.3                                ...           3 years ago
  • 4.0.0-rc.2                                ...           3 years ago
  • 4.0.0-rc.1                                ...           3 years ago
  • 4.0.0-rc.0                                ...           3 years ago
  • 4.0.0-next.7                                ...           3 years ago
  • 4.0.0-next.6                                ...           3 years ago
  • 4.0.0-next.5                                ...           3 years ago
  • 4.0.0-next.4                                ...           3 years ago
  • 4.0.0-next.3                                ...           4 years ago
  • 4.0.0-next.2                                ...           4 years ago
  • 4.0.0-next.1 [deprecated]           ...           4 years ago
  • 4.0.0-next.0 [deprecated]           ...           4 years ago
  • 3.1.2                                ...           4 years ago
  • 3.1.1                                ...           4 years ago
  • 3.1.0                                ...           4 years ago
  • 3.0.2                                ...           5 years ago
  • 3.0.1                                ...           5 years ago
  • 3.0.0                                ...           5 years ago
  • 2.8.0                                ...           7 years ago
  • 2.7.1                                ...           8 years ago
  • 2.7.0                                ...           8 years ago
  • 2.6.5                                ...           8 years ago
  • 2.6.4                                ...           8 years ago
  • 2.6.3                                ...           8 years ago
  • 2.6.2                                ...           9 years ago
  • 2.6.1                                ...           9 years ago
  • 2.6.0                                ...           9 years ago
  • 2.6.0-2                                ...           9 years ago
  • 2.6.0-1                                ...           9 years ago
  • 2.6.0-0                                ...           9 years ago
  • 2.5.3                                ...           9 years ago
  • 2.5.2                                ...           9 years ago
  • 2.5.1                                ...           9 years ago
  • 2.5.0                                ...           9 years ago
  • 2.5.0-1                                ...           9 years ago
  • 2.4.2                                ...           9 years ago
  • 2.4.1                                ...           9 years ago
  • 2.4.0                                ...           9 years ago
  • 2.3.1                                ...           9 years ago
  • 2.3.0                                ...           9 years ago
  • 2.2.5                                ...           9 years ago
  • 2.2.4                                ...           9 years ago
  • 2.2.3                                ...           9 years ago
  • 2.2.2                                ...           9 years ago
  • 2.2.1                                ...           9 years ago
  • 2.2.0                                ...           9 years ago
  • 2.1.0                                ...           9 years ago
  • 2.0.1                                ...           9 years ago
  • 2.0.0                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
  • 0.12.1                                ...           10 years ago
  • 0.12.0                                ...           10 years ago
  • 0.11.0                                ...           10 years ago
  • 0.10.3                                ...           11 years ago
  • 0.10.2                                ...           11 years ago
  • 0.10.1                                ...           11 years ago
  • 0.10.0                                ...           11 years ago
  • 0.9.2                                ...           11 years ago
  • 0.9.1                                ...           11 years ago
  • 0.9.0                                ...           11 years ago
  • 0.8.6                                ...           11 years ago
  • 0.8.5                                ...           11 years ago
  • 0.8.4                                ...           11 years ago
  • 0.8.3                                ...           12 years ago
  • 0.8.2                                ...           12 years ago
  • 0.8.1                                ...           12 years ago
  • 0.8.0                                ...           12 years ago
  • 0.7.3                                ...           12 years ago
  • 0.7.2                                ...           13 years ago
  • 0.7.1                                ...           13 years ago
  • 0.7.0                                ...           13 years ago
  • 0.6.7                                ...           13 years ago
  • 0.6.6                                ...           13 years ago
  • 0.6.5                                ...           13 years ago
  • 0.6.4                                ...           13 years ago
  • 0.6.3                                ...           13 years ago
  • 0.6.2                                ...           13 years ago
  • 0.6.1                                ...           13 years ago
  • 0.6.0                                ...           14 years ago
  • 0.5.11                                ...           14 years ago
  • 0.5.10                                ...           14 years ago
  • 0.5.9                                ...           14 years ago
  • 0.5.8                                ...           14 years ago
  • 0.5.7                                ...           14 years ago
  • 0.5.6                                ...           14 years ago
  • 0.5.5                                ...           14 years ago
  • 0.5.4                                ...           14 years ago
  • 0.5.3                                ...           14 years ago
  • 0.5.2                                ...           14 years ago
  • 0.5.1                                ...           14 years ago
  • 0.5.0                                ...           14 years ago
  • 0.4.2                                ...           14 years ago
  • 0.4.1                                ...           14 years ago
  • 0.4.0                                ...           14 years ago
  • 0.3.9                                ...           14 years ago
  • 0.3.7                                ...           14 years ago
  • 0.3.6                                ...           14 years ago
  • 0.3.5                                ...           14 years ago
  • 0.3.4                                ...           14 years ago
  • 0.3.3                                ...           14 years ago
  • 0.3.2                                ...           14 years ago
  • 0.3.1                                ...           14 years ago
  • 0.3.0                                ...           14 years ago
  • 0.2.6                                ...           14 years ago
  • 0.2.4                                ...           14 years ago
  • 0.2.3                                ...           14 years ago
  • 0.2.2                                ...           14 years ago
  • 0.2.1                                ...           14 years ago
  • 0.2.0                                ...           14 years ago
  • 0.1.2                                ...           14 years ago
  • 0.1.1                                ...           14 years ago
  • 0.1.0                                ...           14 years ago
  • 0.0.8                                ...           14 years ago
  • 0.0.7                                ...           14 years ago
  • 0.0.4                                ...           14 years ago
  • 0.0.3                                ...           14 years ago
  • 0.0.2                                ...           14 years ago
  • 0.0.1                                ...           14 years ago
Downloads
Total 83
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (6)
Dev Dependencies (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE