Skip to content

Commit e82e84e

Browse files
Merge pull request #1528 from redis/DOC-5219-node-redis-v500-updates
DOC-5219 implemented suggested node-redis updates for v5.0.0
2 parents f5cff43 + a6f635a commit e82e84e

File tree

4 files changed

+60
-33
lines changed

4 files changed

+60
-33
lines changed

content/develop/clients/nodejs/connect.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ await cluster.set('foo', 'bar');
9494
const value = await cluster.get('foo');
9595
console.log(value); // returns 'bar'
9696

97-
await cluster.quit();
97+
await cluster.close();
9898
```
9999

100100
## Connect to your production Redis with TLS
@@ -123,15 +123,21 @@ await client.set('foo', 'bar');
123123
const value = await client.get('foo');
124124
console.log(value) // returns 'bar'
125125

126-
await client.disconnect();
126+
await client.destroy();
127127
```
128128

129129
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).
130130

131131
## Reconnect after disconnection
132132

133-
By default, `node-redis` doesn't attempt to reconnect automatically when
134-
the connection to the server is lost. However, you can set the
133+
`node-redis` can attempt to reconnect automatically when
134+
the connection to the server is lost. By default, it will retry
135+
the connection using an
136+
[exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff)
137+
strategy with some random "jitter" added to avoid multiple
138+
clients retrying in sync with each other.
139+
140+
You can also set the
135141
`socket.reconnectionStrategy` field in the configuration to decide
136142
whether to try to reconnect and how to approach it. Choose one of the following values for
137143
`socket.reconnectionStrategy`:
@@ -159,19 +165,18 @@ from the function can be any of the following:
159165
no attempt was made to reconnect.
160166

161167
The example below shows a `reconnectionStrategy` function that implements a
162-
custom [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff)
163-
strategy:
168+
custom exponential backoff strategy:
164169

165170
```js
166171
createClient({
167172
socket: {
168173
reconnectStrategy: retries => {
169-
// Generate a random jitter between 0 – 200 ms:
170-
const jitter = Math.floor(Math.random() * 200);
174+
// Generate a random jitter between 0 – 100 ms:
175+
const jitter = Math.floor(Math.random() * 100);
171176

172-
// Delay is an exponential back off, (times^2) * 50 ms, with a
173-
// maximum value of 2000 ms:
174-
const delay = Math.min(Math.pow(2, retries) * 50, 2000);
177+
// Delay is an exponential backoff, (2^retries) * 50 ms, with a
178+
// maximum value of 3000 ms:
179+
const delay = Math.min(Math.pow(2, retries) * 50, 3000);
175180

176181
return delay + jitter;
177182
}
@@ -193,6 +198,12 @@ related to connection:
193198
parameter. This is usually a network issue such as "Socket closed unexpectedly".
194199
- `reconnecting`: (No parameters) The client is about to try reconnecting after the
195200
connection was lost due to an error.
201+
- `sharded-channel-moved`: The cluster slot of a subscribed
202+
[sharded pub/sub channel]({{< relref "/develop/interact/pubsub#sharded-pubsub" >}})
203+
has been moved to another shard. Note that when you use a
204+
[`RedisCluster`](#connect-to-a-redis-cluster) connection, this event is automatically
205+
handled for you. See
206+
[`sharded-channel-moved` event](https://github.com/redis/node-redis/blob/master/docs/pub-sub.md#sharded-channel-moved-event) for more information.
196207

197208
Use code like the following to respond to these events:
198209

content/develop/clients/nodejs/queryjson.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ Add the following dependencies:
3434
```js
3535
import {
3636
createClient,
37-
SchemaFieldTypes,
38-
AggregateGroupByReducers,
39-
AggregateSteps,
37+
SCHEMA_FIELD_TYPE,
38+
FT_AGGREGATE_GROUP_BY_REDUCERS,
39+
FT_AGGREGATE_STEPS,
4040
} from 'redis';
4141
```
4242

content/develop/clients/nodejs/transpipe.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,27 +156,39 @@ console.log(updatedPath);
156156
```
157157

158158
In an environment where multiple concurrent requests are sharing a connection
159-
(such as a web server), you must wrap the above transaction logic in a call to
160-
`client.executeIsolated` to get an isolated connection, like so:
159+
(such as a web server), you must use a connection pool to get an isolated connection,
160+
as shown below:
161161

162162
```js
163-
await client.executeIsolated(async (client) => {
164-
await client.watch('shellpath');
165-
// ...
166-
})
163+
import { createClientPool } from 'redis';
164+
165+
const pool = await createClientPool()
166+
.on('error', err => console.error('Redis Client Pool Error', err));
167+
168+
try {
169+
await pool.execute(async client => {
170+
await client.watch('key');
171+
172+
const multi = client.multi()
173+
.ping()
174+
.get('key');
175+
176+
if (Math.random() > 0.5) {
177+
await client.watch('another-key');
178+
multi.set('another-key', await client.get('another-key') / 2);
179+
}
180+
181+
return multi.exec();
182+
});
183+
} catch (err) {
184+
if (err instanceof WatchError) {
185+
// the transaction aborted
186+
}
187+
}
167188
```
168189

169190
This is important because the server tracks the state of the WATCH on a
170191
per-connection basis, and concurrent WATCH and MULTI/EXEC calls on the same
171-
connection will interfere with one another.
172-
173-
You can configure the size of the isolation pool when calling `createClient`:
174-
175-
```js
176-
const client = createClient({
177-
isolationPoolOptions: {
178-
min: 1,
179-
max: 100,
180-
},
181-
})
182-
```
192+
connection will interfere with one another. See
193+
[`RedisClientPool`](https://github.com/redis/node-redis/blob/master/docs/pool.md)
194+
for more information.

content/develop/clients/nodejs/vecsearch.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ In your JavaScript source file, import the required classes:
5050

5151
```js
5252
import * as transformers from '@xenova/transformers';
53-
import {VectorAlgorithms, createClient, SchemaFieldTypes} from 'redis';
53+
import {
54+
VectorAlgorithms,
55+
createClient,
56+
SCHEMA_FIELD_TYPE,
57+
} from 'redis';
5458
```
5559

5660
The `@xenova/transformers` module handles embedding models. This example uses the

0 commit comments

Comments
 (0)