Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Production usage docs for Node-Redis #2696

Merged
merged 8 commits into from
Mar 26, 2024
Merged
Changes from 1 commit
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
193 changes: 51 additions & 142 deletions docs/connect/clients/nodejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,166 +136,75 @@ 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).

### Example: Indexing and querying JSON documents
### Production usage

Make sure that you have Redis Stack and `node-redis` installed. Import dependencies:
#### 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.
uglide marked this conversation as resolved.
Show resolved Hide resolved
This event is triggered whenever an error occurs within the client.

```js
import {AggregateSteps, AggregateGroupByReducers, createClient, SchemaFieldTypes} from 'redis';
const client = createClient();
await client.connect();
```

Create an index.

```js
try {
await client.ft.create('idx:users', {
'$.name': {
type: SchemaFieldTypes.TEXT,
SORTABLE: true
},
'$.city': {
type: SchemaFieldTypes.TEXT,
AS: 'city'
},
'$.age': {
type: SchemaFieldTypes.NUMERIC,
AS: 'age'
}
}, {
ON: 'JSON',
PREFIX: 'user:'
});
} catch (e) {
if (e.message === 'Index already exists') {
console.log('Index exists already, skipped creation.');
} else {
// Something went wrong, perhaps RediSearch isn't installed...
console.error(e);
process.exit(1);
}
}
```
**It is crucial to listen for error events.**
uglide marked this conversation as resolved.
Show resolved Hide resolved

Create JSON documents to add to your database.
If a client does not have at least one error listener registered and an error occurs, that error will be thrown, potentially causing the Node.js process to exit unexpectedly.
uglide marked this conversation as resolved.
Show resolved Hide resolved
See [the EventEmitter docs](https://nodejs.org/api/events.html#events_error_events) for more details.

```js
await Promise.all([
client.json.set('user:1', '$', {
"name": "Paul John",
"email": "[email protected]",
"age": 42,
"city": "London"
}),
client.json.set('user:2', '$', {
"name": "Eden Zamir",
"email": "[email protected]",
"age": 29,
"city": "Tel Aviv"
}),
client.json.set('user:3', '$', {
"name": "Paul Zamir",
"email": "[email protected]",
"age": 35,
"city": "Tel Aviv"
}),
]);
```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}`)
});
```

Let's find user 'Paul` and filter the results by age.

```js
let result = await client.ft.search(
'idx:users',
'Paul @age:[30 40]'
);
console.log(JSON.stringify(result, null, 2));
/*
{
"total": 1,
"documents": [
{
"id": "user:3",
"value": {
"name": "Paul Zamir",
"email": "[email protected]",
"age": 35,
"city": "Tel Aviv"
}
}
]
}
*/
```
#### Handling reconnections

Return only the city field.
If the socket unexpectedly closes, such as due to network issues, the client rejects all commands already sent, as they might have been executed on the server.
The rest of pending commands will remain queued in memory until a new socket is established.
uglide marked this conversation as resolved.
Show resolved Hide resolved
uglide marked this conversation as resolved.
Show resolved Hide resolved
The client uses `reconnectStrategy` to decide when to attempt to reconnect.
The default strategy is to calculate 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:
uglide marked this conversation as resolved.
Show resolved Hide resolved

```js
result = await client.ft.search(
'idx:users',
'Paul @age:[30 40]',
{
RETURN: ['$.city']
}
);
console.log(JSON.stringify(result, null, 2));

/*
{
"total": 1,
"documents": [
{
"id": "user:3",
"value": {
"$.city": "Tel Aviv"
}
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}`)
uglide marked this conversation as resolved.
Show resolved Hide resolved
});
```

Count all users in the same city.
In the provided reconnection strategy callback, the client attempts to reconnect up to 20 times with a delay of `retries * 500` milliseconds between attempts.
uglide marked this conversation as resolved.
Show resolved Hide resolved
After approximately 2 minutes, the client logs an error message and terminates the connection if the maximum retry limit is exceeded.
uglide marked this conversation as resolved.
Show resolved Hide resolved

```js
result = await client.ft.aggregate('idx:users', '*', {
STEPS: [
{
type: AggregateSteps.GROUPBY,
properties: ['@city'],
REDUCE: [
{
type: AggregateGroupByReducers.COUNT,
AS: 'count'
}
]
}
]
})
console.log(JSON.stringify(result, null, 2));
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.

/*
{
"total": 2,
"results": [
{
"city": "London",
"count": "1"
},
{
"city": "Tel Aviv",
"count": "2"
}
]
}
*/
#### Timeout

await client.quit();
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)
Expand Down
Loading