-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathnodejs.md
214 lines (159 loc) · 6.85 KB
/
nodejs.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
---
title: "Node.js guide"
linkTitle: "Node.js"
description: Connect your Node.js application to a Redis database
weight: 4
aliases:
- /docs/clients/nodejs/
- /docs/redis-clients/nodejs/
---
Install Redis and the Redis client, then connect your Node.js application to a Redis database.
## node-redis
[node-redis](https://github.com/redis/node-redis) is a modern, high-performance Redis client for Node.js.
`node-redis` requires a running Redis or [Redis Stack](https://redis.io/docs/getting-started/install-stack/) server. See [Getting started](/docs/getting-started/) for Redis installation instructions.
### Install
To install node-redis, run:
```
npm install redis
```
### Connect
Connect to localhost on port 6379.
```js
import { createClient } from 'redis';
const client = createClient();
client.on('error', err => console.log('Redis Client Error', err));
await client.connect();
```
Store and retrieve a simple string.
```js
await client.set('key', 'value');
const value = await client.get('key');
```
Store and retrieve a map.
```js
await client.hSet('user-session:123', {
name: 'John',
surname: 'Smith',
company: 'Redis',
age: 29
})
let userSession = await client.hGetAll('user-session:123');
console.log(JSON.stringify(userSession, null, 2));
/*
{
"surname": "Smith",
"name": "John",
"company": "Redis",
"age": "29"
}
*/
```
To connect to a different host or port, use a connection string in the format `redis[s]://[[username][:password]@][host][:port][/db-number]`:
```js
createClient({
url: 'redis://alice:foobared@awesome.redis.server:6380'
});
```
To check if 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).
#### Connect to a Redis cluster
To connect to a Redis cluster, use `createCluster`.
```js
import { createCluster } from 'redis';
const cluster = createCluster({
rootNodes: [
{
url: 'redis://127.0.0.1:16379'
},
{
url: 'redis://127.0.0.1:16380'
},
// ...
]
});
cluster.on('error', (err) => console.log('Redis Cluster Error', err));
await cluster.connect();
await cluster.set('foo', 'bar');
const value = await cluster.get('foo');
console.log(value); // returns 'bar'
await cluster.quit();
```
#### Connect to your production Redis with TLS
When you deploy your application, use TLS and follow the [Redis security](/docs/management/security/) guidelines.
```js
const client = createClient({
username: 'default', // use your Redis user. More info https://redis.io/docs/management/security/acl/
password: 'secret', // use your password here
socket: {
host: 'my-redis.cloud.redislabs.com',
port: 6379,
tls: true,
key: readFileSync('./redis_user_private.key'),
cert: readFileSync('./redis_user.crt'),
ca: [readFileSync('./redis_ca.pem')]
}
});
client.on('error', (err) => console.log('Redis Client Error', err));
await client.connect();
await client.set('foo', 'bar');
const value = await client.get('foo');
console.log(value) // returns 'bar'
await client.disconnect();
```
You can also use discrete parameters and UNIX sockets. Details can be found in the [client configuration guide](https://github.com/redis/node-redis/blob/master/docs/client-configuration.md).
### Production usage
#### Handling errors
Node-Redis provides [multiple events to handle various scenarios](https://github.com/redis/node-redis?tab=readme-ov-file#events), among which the most critical is the `error` event.
This event is triggered whenever an error occurs within the client.
It is crucial to listen for error events.
If a client does not register at least one error listener and an error occurs, the system will throw that error, potentially causing the Node.js process to exit unexpectedly.
See [the EventEmitter docs](https://nodejs.org/api/events.html#events_error_events) for more details.
```typescript
const client = createClient({
// ... client options
});
// Always ensure there's a listener for errors in the client to prevent process crashes due to unhandled errors
client.on('error', error => {
console.error(`Redis client error:`, error);
});
```
#### Handling reconnections
If network issues or other problems unexpectedly close the socket, the client will reject all commands already sent, since the server might have already executed them.
The rest of the pending commands will remain queued in memory until a new socket is established.
This behaviour is controlled by the `enableOfflineQueue` option, which is enabled by default.
The client uses `reconnectStrategy` to decide when to attempt to reconnect.
The default strategy is to calculate the delay before each attempt based on the attempt number `Math.min(retries * 50, 500)`. You can customize this strategy by passing a supported value to `reconnectStrategy` option:
1. Define a callback `(retries: number, cause: Error) => false | number | Error` **(recommended)**
```typescript
const client = createClient({
socket: {
reconnectStrategy: function(retries) {
if (retries > 20) {
console.log("Too many attempts to reconnect. Redis connection was terminated");
return new Error("Too many retries.");
} else {
return retries * 500;
}
}
}
});
client.on('error', error => console.error('Redis client error:', error));
```
In the provided reconnection strategy callback, the client attempts to reconnect up to 20 times with a delay of `retries * 500` milliseconds between attempts.
After approximately two minutes, the client logs an error message and terminates the connection if the maximum retry limit is exceeded.
2. Use a numerical value to set a fixed delay in milliseconds.
3. Use `false` to disable reconnection attempts. This option should only be used for testing purposes.
#### Timeout
To set a timeout for a connection, use the `connectTimeout` option:
```typescript
const client = createClient({
// setting a 10-second timeout
connectTimeout: 10000 // in milliseconds
});
client.on('error', error => console.error('Redis client error:', error));
```
### Learn more
* [Node-Redis Configuration Options](https://github.com/redis/node-redis/blob/master/docs/client-configuration.md)
* [Redis commands](https://redis.js.org/#node-redis-usage-redis-commands)
* [Programmability](https://redis.js.org/#node-redis-usage-programmability)
* [Clustering](https://redis.js.org/#node-redis-usage-clustering)
* [GitHub](https://github.com/redis/node-redis)