You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/connect/clients/nodejs.md
+50-139
Original file line number
Diff line number
Diff line change
@@ -136,166 +136,77 @@ await client.disconnect();
136
136
137
137
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).
138
138
139
-
### Example: Indexing and querying JSON documents
139
+
### Production usage
140
140
141
-
Make sure that you have Redis Stack and `node-redis` installed. Import dependencies:
141
+
#### Handling errors
142
+
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.
// Something went wrong, perhaps RediSearch isn't installed...
175
-
console.error(e);
176
-
process.exit(1);
177
-
}
178
-
}
179
-
```
180
148
181
-
Create JSON documents to add to your database.
149
+
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.
150
+
See [the EventEmitter docs](https://nodejs.org/api/events.html#events_error_events) for more details.
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.
166
+
The rest of the pending commands will remain queued in memory until a new socket is established.
167
+
This behaviour is controlled by the `enableOfflineQueue` option, which is enabled by default.
233
168
234
-
```js
235
-
result =awaitclient.ft.search(
236
-
'idx:users',
237
-
'Paul @age:[30 40]',
238
-
{
239
-
RETURN: ['$.city']
240
-
}
241
-
);
242
-
console.log(JSON.stringify(result, null, 2));
169
+
The client uses `reconnectStrategy` to decide when to attempt to reconnect.
170
+
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:
243
171
244
-
/*
245
-
{
246
-
"total": 1,
247
-
"documents": [
248
-
{
249
-
"id": "user:3",
250
-
"value": {
251
-
"$.city": "Tel Aviv"
252
-
}
172
+
173
+
1. Define a callback `(retries: number, cause: Error) => false | number | Error`**(recommended)**
174
+
```typescript
175
+
const client =createClient({
176
+
socket: {
177
+
reconnectStrategy: function(retries) {
178
+
if (retries>20) {
179
+
console.log("Too many attempts to reconnect. Redis connection was terminated");
In the provided reconnection strategy callback, the client attempts to reconnect up to 20 times with a delay of `retries * 500` milliseconds between attempts.
190
+
After approximately two minutes, the client logs an error message and terminates the connection if the maximum retry limit is exceeded.
260
191
261
-
```js
262
-
result =awaitclient.ft.aggregate('idx:users', '*', {
263
-
STEPS: [
264
-
{
265
-
type:AggregateSteps.GROUPBY,
266
-
properties: ['@city'],
267
-
REDUCE: [
268
-
{
269
-
type:AggregateGroupByReducers.COUNT,
270
-
AS:'count'
271
-
}
272
-
]
273
-
}
274
-
]
275
-
})
276
-
console.log(JSON.stringify(result, null, 2));
277
192
278
-
/*
279
-
{
280
-
"total": 2,
281
-
"results": [
282
-
{
283
-
"city": "London",
284
-
"count": "1"
285
-
},
286
-
{
287
-
"city": "Tel Aviv",
288
-
"count": "2"
289
-
}
290
-
]
291
-
}
292
-
*/
193
+
2. Use a numerical value to set a fixed delay in milliseconds.
194
+
3. Use `false` to disable reconnection attempts. This option should only be used for testing purposes.
195
+
196
+
#### Timeout
293
197
294
-
awaitclient.quit();
198
+
To set a timeout for a connection, use the `connectTimeout` option:
0 commit comments