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: README.md
+31Lines changed: 31 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -227,6 +227,37 @@ It also supports an additional `rewriteRequestHeaders(headers, request)` functio
227
227
opening the WebSocket connection. This function should return an object with the given headers.
228
228
The default implementation forwards the `cookie` header.
229
229
230
+
## `wsReconnect`
231
+
232
+
**Experimental.** (default: `disabled`)
233
+
234
+
Reconnection feature detects and closes broken connections and reconnects automatically, see [how to detect and close broken connections](https://github.com/websockets/ws#how-to-detect-and-close-broken-connections).
235
+
The connection is considered broken if the target does not respond to the ping messages or no data is received from the target.
236
+
237
+
The `wsReconnect` option contains the configuration for the WebSocket reconnection feature.
238
+
To enable the feature, set the `wsReconnect` option to an object with the following properties:
239
+
240
+
-`pingInterval`: The interval between ping messages in ms (default: `30_000`).
241
+
-`maxReconnectionRetries`: The maximum number of reconnection retries (`1` to `Infinity`, default: `Infinity`).
242
+
-`reconnectInterval`: The interval between reconnection attempts in ms (default: `1_000`).
243
+
-`reconnectDecay`: The decay factor for the reconnection interval (default: `1.5`).
244
+
-`connectionTimeout`: The timeout for establishing the connection in ms (default: `5_000`).
245
+
-`reconnectOnClose`: Whether to reconnect on close, as long as the connection from the related client to the proxy is active (default: `false`).
246
+
-`logs`: Whether to log the reconnection process (default: `false`).
247
+
248
+
See the example in [examples/reconnection](examples/reconnection).
249
+
250
+
## wsHooks
251
+
252
+
On websocket events, the following hooks are available, note **the hooks are all synchronous**.
253
+
254
+
-`onIncomingMessage`: A hook function that is called when the request is received from the client `onIncomingMessage(source, target, { data, binary })` (default: `undefined`).
255
+
-`onOutgoingMessage`: A hook function that is called when the response is received from the target `onOutgoingMessage(source, target, { data, binary })` (default: `undefined`).
256
+
-`onConnect`: A hook function that is called when the connection is established `onConnect(source, target)` (default: `undefined`).
257
+
-`onDisconnect`: A hook function that is called when the connection is closed `onDisconnect(source)` (default: `undefined`).
258
+
-`onReconnect`: A hook function that is called when the connection is reconnected `onReconnect(source, target)` (default: `undefined`). The function is called if reconnection feature is enabled.
259
+
-`onPong`: A hook function that is called when the target responds to the ping `onPong(source, target)` (default: `undefined`). The function is called if reconnection feature is enabled.
260
+
230
261
## Benchmarks
231
262
232
263
The following benchmarks were generated on a dedicated server with an Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz and 64GB of RAM:
This example demonstrates how to use the reconnection feature of the proxy.
4
+
5
+
It simulates an unstable target service: slow to start, unresponsive due to block of the event loop, crash and restart.
6
+
7
+
The goal is to ensures a more resilient and customizable integration, minimizing disruptions caused by connection instability.
8
+
9
+
10
+
## How to run
11
+
12
+
Run the unstable target
13
+
14
+
```
15
+
cd examples/reconnection/unstable-target
16
+
npm run unstable
17
+
```
18
+
19
+
Run the proxy
20
+
21
+
```
22
+
cd examples/reconnection/proxy
23
+
npm run start
24
+
```
25
+
26
+
Then run the client
27
+
28
+
```
29
+
cd examples/reconnection/client
30
+
npm run start
31
+
```
32
+
33
+
---
34
+
35
+
## How it works
36
+
37
+
### Proxy Connection Monitoring and Recovery
38
+
39
+
The proxy monitors the target connection using a ping/pong mechanism. If a pong response does not arrive on time, the connection is closed, and the proxy attempts to reconnect.
40
+
41
+
If the target service crashes, the connection may close either gracefully or abruptly. Regardless of how the disconnection occurs, the proxy detects the connection loss and initiates a reconnection attempt.
42
+
43
+
### Connection Stability
44
+
45
+
- The connection between the client and the proxy remains unaffected by an unstable target.
46
+
- The connection between the proxy and the target may be closed due to:
47
+
- The target failing to respond to ping messages, even if the connection is still technically open (e.g., due to a freeze or blockage).
48
+
- The target crashing and restarting.
49
+
50
+
### Handling Data Loss During Reconnection
51
+
52
+
The proxy supports hooks to manage potential data loss during reconnection. These hooks allow for custom logic to ensure message integrity when resending data from the client to the target.
53
+
54
+
Examples of how hooks can be used based on the target service type:
55
+
56
+
- GraphQL subscriptions: Resend the subscription from the last received message.
57
+
- Message brokers: Resend messages starting from the last successfully processed message.
58
+
59
+
In this example, the proxy re-sends the messages from the last ping to ensure all the messages are sent to the target, without any additional logic.
60
+
Resending messages from the last pong ensures that the target does not miss any messages, but it may send messages more than once.
0 commit comments