Skip to content

Commit 71a2ec2

Browse files
Merge pull request #1260 from redis/DOC-4942-ioredis-migration-docs
DOC-4942 ioredis migration docs
2 parents 5d5a380 + c1f649f commit 71a2ec2

File tree

3 files changed

+455
-4
lines changed

3 files changed

+455
-4
lines changed

content/develop/clients/nodejs/connect.md

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ createClient({
6767
```
6868
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).
6969

70-
### Connect to a Redis cluster
70+
## Connect to a Redis cluster
7171

7272
To connect to a Redis cluster, use `createCluster`.
7373

@@ -97,7 +97,7 @@ console.log(value); // returns 'bar'
9797
await cluster.quit();
9898
```
9999

100-
### Connect to your production Redis with TLS
100+
## Connect to your production Redis with TLS
101101

102102
When you deploy your application, use TLS and follow the [Redis security]({{< relref "/operate/oss_and_stack/management/security/" >}}) guidelines.
103103

@@ -127,3 +127,77 @@ await client.disconnect();
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).
130+
131+
## Reconnect after disconnection
132+
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
135+
`socket.reconnectionStrategy` field in the configuration to decide
136+
whether to try to reconnect and how to approach it. Choose one of the following values for
137+
`socket.reconnectionStrategy`:
138+
139+
- `false`: (Default) Don't attempt to reconnect.
140+
- `number`: Wait for this number of milliseconds and then attempt to reconnect.
141+
- `<function>`: Use a custom
142+
function to decide how to handle reconnection.
143+
144+
The custom function has the following signature:
145+
146+
```js
147+
(retries: number, cause: Error) => false | number | Error
148+
```
149+
150+
It is called before each attempt to reconnect, with the `retries`
151+
indicating how many attempts have been made so far. The `cause` parameter is an
152+
[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
153+
object with information about how the connection was lost. The return value
154+
from the function can be any of the following:
155+
156+
- `false`: Don't attempt to reconnect.
157+
- `number`: Wait this number of milliseconds and then try again.
158+
- `Error`: Same as `false`, but lets you supply extra information about why
159+
no attempt was made to reconnect.
160+
161+
The example below shows a `reconnectionStrategy` function that implements a
162+
custom [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff)
163+
strategy:
164+
165+
```js
166+
createClient({
167+
socket: {
168+
reconnectStrategy: retries => {
169+
// Generate a random jitter between 0 – 200 ms:
170+
const jitter = Math.floor(Math.random() * 200);
171+
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);
175+
176+
return delay + jitter;
177+
}
178+
}
179+
});
180+
```
181+
182+
## Connection events
183+
184+
The client object emits the following
185+
[events](https://developer.mozilla.org/en-US/docs/Web/API/Event) that are
186+
related to connection:
187+
188+
- `connect`: (No parameters) The client is about to start connecting to the server.
189+
- `ready`: (No parameters) The client has connected and is ready to use.
190+
- `end`: (No parameters) The client has been intentionally closed using `client.quit()`.
191+
- `error`: An error has occurred, which is described by the
192+
[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
193+
parameter. This is usually a network issue such as "Socket closed unexpectedly".
194+
- `reconnecting`: (No parameters) The client is about to try reconnecting after the
195+
connection was lost due to an error.
196+
197+
Use code like the following to respond to these events:
198+
199+
```js
200+
client.on('error', error => {
201+
console.error(`Redis client error:`, error);
202+
});
203+
```

0 commit comments

Comments
 (0)