Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions content/develop/clients/nodejs/connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ await cluster.set('foo', 'bar');
const value = await cluster.get('foo');
console.log(value); // returns 'bar'

await cluster.quit();
await cluster.close();
```

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

await client.disconnect();
await client.destroy();
```

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

## Reconnect after disconnection

By default, `node-redis` doesn't attempt to reconnect automatically when
the connection to the server is lost. However, you can set the
`node-redis` can attempt to reconnect automatically when
the connection to the server is lost. By default, it will retry
the connection using an
[exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff)
strategy with some random "jitter" added to avoid multiple
clients retrying in sync with each other.

You can also set the
`socket.reconnectionStrategy` field in the configuration to decide
whether to try to reconnect and how to approach it. Choose one of the following values for
`socket.reconnectionStrategy`:
Expand Down Expand Up @@ -159,19 +165,18 @@ from the function can be any of the following:
no attempt was made to reconnect.

The example below shows a `reconnectionStrategy` function that implements a
custom [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff)
strategy:
custom exponential backoff strategy:

```js
createClient({
socket: {
reconnectStrategy: retries => {
// Generate a random jitter between 0 – 200 ms:
const jitter = Math.floor(Math.random() * 200);
// Generate a random jitter between 0 – 100 ms:
const jitter = Math.floor(Math.random() * 100);

// Delay is an exponential back off, (times^2) * 50 ms, with a
// maximum value of 2000 ms:
const delay = Math.min(Math.pow(2, retries) * 50, 2000);
// Delay is an exponential backoff, (2^retries) * 50 ms, with a
// maximum value of 3000 ms:
const delay = Math.min(Math.pow(2, retries) * 50, 3000);

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

Use code like the following to respond to these events:

Expand Down
6 changes: 3 additions & 3 deletions content/develop/clients/nodejs/queryjson.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ Add the following dependencies:
```js
import {
createClient,
SchemaFieldTypes,
AggregateGroupByReducers,
AggregateSteps,
SCHEMA_FIELD_TYPE,
FT_AGGREGATE_GROUP_BY_REDUCERS,
FT_AGGREGATE_STEPS,
} from 'redis';
```

Expand Down
48 changes: 30 additions & 18 deletions content/develop/clients/nodejs/transpipe.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,27 +156,39 @@ console.log(updatedPath);
```

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

```js
await client.executeIsolated(async (client) => {
await client.watch('shellpath');
// ...
})
import { createClientPool } from 'redis';

const pool = await createClientPool()
.on('error', err => console.error('Redis Client Pool Error', err));

try {
await pool.execute(async client => {
await client.watch('key');

const multi = client.multi()
.ping()
.get('key');

if (Math.random() > 0.5) {
await client.watch('another-key');
multi.set('another-key', await client.get('another-key') / 2);
}

return multi.exec();
});
} catch (err) {
if (err instanceof WatchError) {
// the transaction aborted
}
}
```

This is important because the server tracks the state of the WATCH on a
per-connection basis, and concurrent WATCH and MULTI/EXEC calls on the same
connection will interfere with one another.

You can configure the size of the isolation pool when calling `createClient`:

```js
const client = createClient({
isolationPoolOptions: {
min: 1,
max: 100,
},
})
```
connection will interfere with one another. See
[`RedisClientPool`](https://github.com/redis/node-redis/blob/master/docs/pool.md)
for more information.
6 changes: 5 additions & 1 deletion content/develop/clients/nodejs/vecsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ In your JavaScript source file, import the required classes:

```js
import * as transformers from '@xenova/transformers';
import {VectorAlgorithms, createClient, SchemaFieldTypes} from 'redis';
import {
VectorAlgorithms,
createClient,
SCHEMA_FIELD_TYPE,
} from 'redis';
```

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