websocket-ts 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +225 -212
  3. package/dist/cjs/src/backoff/exponentialbackoff.js +1 -1
  4. package/dist/cjs/src/backoff/exponentialbackoff.js.map +1 -1
  5. package/dist/cjs/src/index.d.ts +1 -1
  6. package/dist/cjs/src/index.d.ts.map +1 -1
  7. package/dist/cjs/src/index.js.map +1 -1
  8. package/dist/cjs/src/websocket.d.ts +9 -3
  9. package/dist/cjs/src/websocket.d.ts.map +1 -1
  10. package/dist/cjs/src/websocket.js +7 -3
  11. package/dist/cjs/src/websocket.js.map +1 -1
  12. package/dist/cjs/src/websocket_builder.d.ts +9 -9
  13. package/dist/cjs/src/websocket_builder.d.ts.map +1 -1
  14. package/dist/cjs/src/websocket_builder.js +6 -6
  15. package/dist/cjs/src/websocket_builder.js.map +1 -1
  16. package/dist/cjs/src/websocket_event.d.ts +18 -7
  17. package/dist/cjs/src/websocket_event.d.ts.map +1 -1
  18. package/dist/cjs/src/websocket_event.js +8 -9
  19. package/dist/cjs/src/websocket_event.js.map +1 -1
  20. package/dist/cjs/src/websocket_retry_options.d.ts +1 -1
  21. package/dist/esm/src/backoff/exponentialbackoff.js +1 -1
  22. package/dist/esm/src/backoff/exponentialbackoff.js.map +1 -1
  23. package/dist/esm/src/index.d.ts +1 -1
  24. package/dist/esm/src/index.d.ts.map +1 -1
  25. package/dist/esm/src/index.js.map +1 -1
  26. package/dist/esm/src/websocket.d.ts +9 -3
  27. package/dist/esm/src/websocket.d.ts.map +1 -1
  28. package/dist/esm/src/websocket.js +7 -3
  29. package/dist/esm/src/websocket.js.map +1 -1
  30. package/dist/esm/src/websocket_builder.d.ts +9 -9
  31. package/dist/esm/src/websocket_builder.d.ts.map +1 -1
  32. package/dist/esm/src/websocket_builder.js +6 -6
  33. package/dist/esm/src/websocket_builder.js.map +1 -1
  34. package/dist/esm/src/websocket_event.d.ts +18 -7
  35. package/dist/esm/src/websocket_event.d.ts.map +1 -1
  36. package/dist/esm/src/websocket_event.js +8 -9
  37. package/dist/esm/src/websocket_event.js.map +1 -1
  38. package/dist/esm/src/websocket_retry_options.d.ts +1 -1
  39. package/package.json +19 -24
  40. package/src/backoff/exponentialbackoff.ts +1 -1
  41. package/src/index.ts +1 -1
  42. package/src/websocket.ts +16 -5
  43. package/src/websocket_builder.ts +10 -10
  44. package/src/websocket_event.ts +22 -7
  45. package/src/websocket_retry_options.ts +1 -1
  46. package/tsconfig.cjs.json +74 -74
  47. package/tsconfig.esm.json +74 -74
  48. package/websocket-ts-2.2.0.tgz +0 -0
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Joscha Behrmann
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Joscha Behrmann
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,212 +1,225 @@
1
- <div>
2
- <div align="center">
3
- <img src="https://raw.githubusercontent.com/jjxxs/websocket-ts/gh-pages/websocket-ts-logo.svg" alt="websocket-ts" width="300" height="65" />
4
- </div>
5
- <p align="center">
6
- <img src="https://github.com/jjxxs/websocket-ts/actions/workflows/build.yml/badge.svg" alt="Build Status" />
7
- <img src="https://github.com/jjxxs/websocket-ts/actions/workflows/test.yml/badge.svg" alt="Build Status" />
8
- <a href="https://coveralls.io/github/jjxxs/websocket-ts?branch=master">
9
- <img src="https://coveralls.io/repos/github/jjxxs/websocket-ts/badge.svg?branch=master&service=github" alt="Coverage Status" />
10
- </a>
11
- <a href="https://github.com/jjxxs/websocket-ts/releases/latest">
12
- <img src="https://img.shields.io/github/v/release/jjxxs/websocket-ts" alt="Release" />
13
- </a>
14
- <a href="/LICENSE">
15
- <img src="https://img.shields.io/github/license/jjxxs/websocket-ts" alt="License" />
16
- </a>
17
- </p>
18
- </div>
19
-
20
- <div align="center">
21
- A <b>WebSocket</b> for browsers with <b>auto-reconnect</b> and <b>message buffering</b> written in <b>TypeScript</b>.
22
- </div>
23
-
24
- ## Features
25
-
26
- - **Lightweight & Standalone**: No dependencies, 2.1 kB minified & gzipped.
27
- - **Browser-native**: Utilizes WebSocket API, offers direct access.
28
- - **Smart Reconnect**: Optional auto-reconnect and message buffering.
29
- - **Easy Setup**: Optional builder class for quick initialization.
30
- - **Well-Tested**: High test coverage, well-documented for extensibility.
31
- - **Module Support**: Supports CommonJS and ES6 modules.
32
-
33
- ## Installation
34
-
35
- Install `websocket-ts` with npm:
36
-
37
- ```bash
38
- $ npm install websocket-ts
39
- ```
40
-
41
- ## Quickstart
42
- This example shows how to use the package, complete with message buffering and automatic reconnection.
43
- The created websocket will echo back any received messages. It will buffer messages when disconnected
44
- and attempt to reconnect every 1 second.
45
-
46
- ```typescript
47
- import {
48
- ArrayQueue,
49
- ConstantBackoff,
50
- Websocket,
51
- WebsocketBuilder,
52
- WebsocketEvent,
53
- } from "websocket-ts";
54
-
55
- // Initialize WebSocket with buffering and 1s reconnection delay
56
- const ws = new WebsocketBuilder("ws://localhost:8080")
57
- .withBuffer(new ArrayQueue()) // buffer messages when disconnected
58
- .withBackoff(new ConstantBackoff(1000)) // retry every 1s
59
- .build();
60
-
61
- // Function to output & echo received messages
62
- const echoOnMessage = (i: Websocket, ev: MessageEvent) => {
63
- console.log(`received message: ${ev.data}`);
64
- i.send(`echo: ${ev.data}`);
65
- };
66
-
67
- // Add event listeners
68
- ws.addEventListener(WebsocketEvent.open, () => console.log("opened!"));
69
- ws.addEventListener(WebsocketEvent.close, () => console.log("closed!"));
70
- ws.addEventListener(WebsocketEvent.message, echoOnMessage);
71
- ```
72
-
73
- ## Usage
74
- This will demonstrate how to use `websocket-ts` in your project using the provided `WebsocketBuild`-class.
75
-
76
- For a more detailed description of the API, please refer to the [API Documentation](https://jjxxs.github.io/websocket-ts/).
77
-
78
- #### Initialization
79
-
80
- Create a new instance with the `WebsocketBuilder`:
81
-
82
- ```typescript
83
- const ws = new WebsocketBuilder("ws://localhost:42421").build();
84
- ```
85
-
86
- #### Events
87
-
88
- There are six events which can be subscribed to through with event listeners:
89
-
90
- ```typescript
91
- export enum WebsocketEvent {
92
- open = "open", // Connection opened
93
- close = "close", // Connection closed
94
- error = "error", // Error-induced closure
95
- message = "message", // Message received
96
- retry = "retry", // Reconnect attempt
97
- reconnect = "reconnect" // Successful reconnect
98
- }
99
- ```
100
-
101
- #### Add Event Listeners
102
- Event listeners receive the websocket instance (`i`) and the triggering event (`ev`) as arguments.
103
-
104
- ```typescript
105
- const ws = new WebsocketBuilder("ws://localhost:42421")
106
- .onOpen((i, ev) => console.log("opened"))
107
- .onClose((i, ev) => console.log("closed"))
108
- .onError((i, ev) => console.log("error"))
109
- .onMessage((i, ev) => console.log("message"))
110
- .onRetry((i, ev) => console.log("retry"))
111
- .onReconnect((i, ev) => console.log("reconnect"))
112
- .build();
113
- ```
114
-
115
- #### Remove Event Listeners
116
-
117
- To unregister a specific event listener, use `removeEventListener`:
118
-
119
- ```typescript
120
- let ws: Websocket
121
- /* ... */
122
- ws.removeEventListener(WebsocketEvent.open, openEventListener);
123
- ```
124
-
125
- #### Send Message
126
-
127
- Use the `send` method to send a message to the server:
128
-
129
- ```typescript
130
- let ws: Websocket;
131
- /* ... */
132
- ws.send("Hello World!");
133
- ```
134
-
135
- #### Reconnect & Backoff (Optional)
136
-
137
- If you'd like the websocket to automatically reconnect upon disconnection, you can optionally provide a `Backoff` strategy.
138
- This sets the delay between reconnection attempts. There are three built-in `Backoff` implementations, or you can create
139
- your own by implementing the `Backoff` interface. If no Backoff is provided, the websocket will not attempt to reconnect.
140
-
141
- ##### ConstantBackoff
142
-
143
- The `ConstantBackoff` strategy enforces a fixed delay between each reconnection attempt.
144
- To set a constant 1-second wait time, use:
145
-
146
- ```typescript
147
- const ws = new WebsocketBuilder("ws://localhost:42421")
148
- .withBackoff(new ConstantBackoff(1000)) // 1000ms = 1s
149
- .build();
150
- ```
151
-
152
- ##### LinearBackoff
153
-
154
- The `LinearBackoff` strategy increases the delay between reconnection attempts linearly,
155
- up to an optional maximum. For example, to start with a 0-second delay and increase by
156
- 10 second for each retry, capping at 60 seconds, use:
157
-
158
- ```typescript
159
- const ws = new WebsocketBuilder("ws://localhost:42421")
160
- .withBackoff(new LinearBackoff(0, 10000, 60000)) // 0ms, 10s, 20s, 30s, 40s, 50s, 60s
161
- .build();
162
- ```
163
-
164
- ##### ExponentialBackoff
165
-
166
- The `ExponentialBackoff` strategy doubles the delay between each reconnection attempt, up
167
- to a specified maximum. This approach is inspired by the binary exponential backoff algorithm
168
- commonly used in networking. For example, to generate a backoff series like `[1s, 2s, 4s, 8s]`, use:
169
-
170
- ```typescript
171
- const ws = new WebsocketBuilder("ws://localhost:42421")
172
- .withBackoff(new ExponentialBackoff(1000, 6)) // 1s, 2s, 4s, 8s, 16s, 32s, 64s
173
- .build();
174
- ```
175
-
176
- #### Buffer (Optional)
177
-
178
- To buffer outgoing messages when the websocket is disconnected, you can optionally specify
179
- a `Queue`. This queue will temporarily store your messages and send them in sequence when
180
- the websocket (re)connects. Two built-in `Queue` implementations are available, or you can
181
- create your own by implementing the `Queue` interface. If no queue is provided, messages
182
- won't be buffered.
183
-
184
- ##### RingQueue
185
-
186
- The `RingQueue` is a fixed-capacity, first-in-first-out (FIFO) queue. When it reaches capacity,
187
- the oldest element is removed to accommodate new ones. Reading from the queue returns and
188
- removes the oldest element. For instance, to set up a `RingQueue` with a 100-element capacity,
189
- use:
190
-
191
- ```typescript
192
- const ws = new WebsocketBuilder("ws://localhost:42421")
193
- .withBuffer(new RingQueue(100))
194
- .build();
195
- ```
196
-
197
- ##### ArrayQueue
198
-
199
- The ArrayQueue offers an unbounded capacity, functioning as a first-in-first-out (FIFO) queue.
200
- Reading from this queue returns and removes the oldest element. To use an `ArrayQueue`, use:
201
-
202
- ```typescript
203
- const ws = new WebsocketBuilder("ws://localhost:42421")
204
- .withBuffer(new ArrayQueue())
205
- .build();
206
- ```
207
-
208
- ## Build & Tests
209
-
210
- To compile the project, execute `npm run build`.
211
-
212
- To run tests, use `npm run test`.
1
+ <div align="center">
2
+ <img src="https://raw.githubusercontent.com/jjxxs/websocket-ts/gh-pages/websocket-ts-logo.svg" alt="websocket-ts" width="300" height="65" />
3
+ </div>
4
+ &nbsp;
5
+ <div align="center">
6
+ A <b>WebSocket</b> for browsers with <b>auto-reconnect</b> and <b>message buffering</b> written in <b>TypeScript</b>.
7
+ </div>
8
+ &nbsp;
9
+ <div align="center">
10
+
11
+ [![Build Status](https://img.shields.io/github/actions/workflow/status/jjxxs/websocket-ts/build.yml)](https://github.com/jjxxs/websocket-ts/actions/workflows/build.yml)
12
+ [![Test Status](https://img.shields.io/github/actions/workflow/status/jjxxs/websocket-ts/test.yml?label=tests)](https://github.com/jjxxs/websocket-ts/actions/workflows/test.yml)
13
+ [![Coverage Status](https://img.shields.io/coveralls/github/jjxxs/websocket-ts/master)](https://coveralls.io/github/jjxxs/websocket-ts?branch=master)
14
+ [![npm downloads](https://img.shields.io/npm/dm/websocket-ts.svg)](https://npm-stat.com/charts.html?package=websocket-ts)
15
+
16
+ </div>
17
+
18
+ <div align="center">
19
+
20
+ [![npm version](https://img.shields.io/npm/v/websocket-ts.svg)](https://www.npmjs.org/package/websocket-ts)
21
+ [![Bundle Size](https://deno.bundlejs.com/badge?q=websocket-ts@2.2.1)](https://bundlejs.com/?q=websocket-ts)
22
+ [![Known Vulnerabilities](https://snyk.io/test/npm/websocket-ts/badge.svg)](https://snyk.io/test/npm/websocket-ts)
23
+ [![License](https://img.shields.io/github/license/jjxxs/websocket-ts)](/LICENSE)
24
+
25
+ </div>
26
+
27
+ ## Features
28
+
29
+ - **Lightweight & Standalone**: No dependencies, 2.5 kB minified & gzipped.
30
+ - **Browser-native**: Utilizes native WebSocket API, offers direct access to the underlying connection.
31
+ - **Smart Reconnect**: Optional auto-reconnect and message buffering.
32
+ - **Easy Setup**: Optional builder class for quick initialization.
33
+ - **Well-Tested**: High test coverage, well-documented for extensibility.
34
+ - **Module Support**: Supports CommonJS and ES6 modules.
35
+
36
+ ## Installation
37
+
38
+ Install `websocket-ts` with npm:
39
+
40
+ ```bash
41
+ npm install websocket-ts
42
+ ```
43
+
44
+ ## Quickstart
45
+ This example creates a WebSocket with message buffering and automatic reconnection.
46
+ The created websocket will echo back any received messages. It will buffer messages when disconnected
47
+ and attempt to reconnect every 1 second.
48
+
49
+ ```typescript
50
+ import {
51
+ ArrayQueue,
52
+ ConstantBackoff,
53
+ Websocket,
54
+ WebsocketBuilder,
55
+ WebsocketEvent,
56
+ } from "websocket-ts";
57
+
58
+ // Initialize WebSocket with buffering and 1s reconnection delay
59
+ const ws = new WebsocketBuilder("ws://localhost:8080")
60
+ .withBuffer(new ArrayQueue()) // buffer messages when disconnected
61
+ .withBackoff(new ConstantBackoff(1000)) // retry every 1s
62
+ .build();
63
+
64
+ // Function to output & echo received messages
65
+ const echoOnMessage = (i: Websocket, ev: MessageEvent) => {
66
+ console.log(`received message: ${ev.data}`);
67
+ i.send(`echo: ${ev.data}`);
68
+ };
69
+
70
+ // Add event listeners
71
+ ws.addEventListener(WebsocketEvent.open, () => console.log("opened!"));
72
+ ws.addEventListener(WebsocketEvent.close, () => console.log("closed!"));
73
+ ws.addEventListener(WebsocketEvent.message, echoOnMessage);
74
+ ```
75
+
76
+ ## Usage
77
+ This section demonstrates how to use `websocket-ts` in your project using the provided `WebsocketBuilder` class.
78
+
79
+ For a more detailed description of the API, please refer to the [API Documentation](https://jjxxs.github.io/websocket-ts/).
80
+
81
+ #### Initialization
82
+
83
+ Create a new instance with the `WebsocketBuilder`:
84
+
85
+ ```typescript
86
+ const ws = new WebsocketBuilder("ws://localhost:42421").build();
87
+ ```
88
+
89
+ #### Events
90
+
91
+ There are six events you can listen for:
92
+
93
+ | Event | Description |
94
+ |---|---|
95
+ | `open` | Connection opened |
96
+ | `close` | Connection closed |
97
+ | `error` | An error occurred |
98
+ | `message` | Message received |
99
+ | `retry` | Reconnect attempt |
100
+ | `reconnect` | Successful reconnect |
101
+
102
+ You can use either `WebsocketEvent.open` or the string `"open"` when registering listeners.
103
+
104
+ #### Add Event Listeners
105
+ Event listeners receive the websocket instance (`i`) and the triggering event (`ev`) as arguments.
106
+
107
+ ```typescript
108
+ const ws = new WebsocketBuilder("ws://localhost:42421")
109
+ .onOpen((i, ev) => console.log("opened"))
110
+ .onClose((i, ev) => console.log("closed"))
111
+ .onError((i, ev) => console.log("error"))
112
+ .onMessage((i, ev) => console.log("message"))
113
+ .onRetry((i, ev) => console.log("retry"))
114
+ .onReconnect((i, ev) => console.log("reconnect"))
115
+ .build();
116
+ ```
117
+
118
+ #### Remove Event Listeners
119
+
120
+ To unregister a specific event listener, use `removeEventListener`:
121
+
122
+ ```typescript
123
+ let ws: Websocket
124
+ /* ... */
125
+ ws.removeEventListener(WebsocketEvent.open, openEventListener);
126
+ ```
127
+
128
+ #### Send Message
129
+
130
+ Use the `send` method to send a message to the server:
131
+
132
+ ```typescript
133
+ let ws: Websocket;
134
+ /* ... */
135
+ ws.send("Hello World!");
136
+ ```
137
+
138
+ #### Reconnect & Backoff (Optional)
139
+
140
+ To automatically reconnect after a disconnection, provide a `Backoff` strategy.
141
+ This controls the delay between reconnection attempts. There are three built-in `Backoff` implementations, or you can create
142
+ your own by implementing the `Backoff` interface. If no backoff is provided, the websocket will not attempt to reconnect.
143
+
144
+ ##### ConstantBackoff
145
+
146
+ `ConstantBackoff` uses a fixed delay between each reconnection attempt.
147
+ To set a constant 1-second wait time, use:
148
+
149
+ ```typescript
150
+ const ws = new WebsocketBuilder("ws://localhost:42421")
151
+ .withBackoff(new ConstantBackoff(1000)) // 1000ms = 1s
152
+ .build();
153
+ ```
154
+
155
+ #### LinearBackoff
156
+
157
+ `LinearBackoff` increases the delay between reconnection attempts linearly,
158
+ up to an optional maximum. For example, to start with a 0-second delay and increase by
159
+ 10 seconds for each retry, capping at 60 seconds, use:
160
+
161
+ ```typescript
162
+ const ws = new WebsocketBuilder("ws://localhost:42421")
163
+ .withBackoff(new LinearBackoff(0, 10000, 60000)) // 0ms, 10s, 20s, 30s, 40s, 50s, 60s
164
+ .build();
165
+ ```
166
+
167
+ ##### ExponentialBackoff
168
+
169
+ `ExponentialBackoff` doubles the delay between each reconnection attempt, up
170
+ to a specified maximum. This approach is inspired by the binary exponential backoff algorithm
171
+ commonly used in networking. For example, to use a base of 1000ms with a maximum exponent of 6:
172
+
173
+ ```typescript
174
+ const ws = new WebsocketBuilder("ws://localhost:42421")
175
+ .withBackoff(new ExponentialBackoff(1000, 6)) // 1s, 2s, 4s, 8s, 16s, 32s, 64s
176
+ .build();
177
+ ```
178
+
179
+ #### Buffer (Optional)
180
+
181
+ To buffer outgoing messages when the websocket is disconnected, provide a `Queue`.
182
+ The queue temporarily stores messages and sends them in order when
183
+ the websocket (re)connects. Two built-in `Queue` implementations are available, or you can
184
+ create your own by implementing the `Queue` interface. If no queue is provided, messages
185
+ won't be buffered.
186
+
187
+ ##### RingQueue
188
+
189
+ `RingQueue` is a fixed-capacity, first-in-first-out (FIFO) queue. When it reaches capacity,
190
+ the oldest element is removed to make room for new ones. For instance, to set up a `RingQueue` with a 100-element capacity:
191
+
192
+ ```typescript
193
+ const ws = new WebsocketBuilder("ws://localhost:42421")
194
+ .withBuffer(new RingQueue(100))
195
+ .build();
196
+ ```
197
+
198
+ ##### ArrayQueue
199
+
200
+ `ArrayQueue` is an unbounded first-in-first-out (FIFO) queue.
201
+ To use an `ArrayQueue`:
202
+
203
+ ```typescript
204
+ const ws = new WebsocketBuilder("ws://localhost:42421")
205
+ .withBuffer(new ArrayQueue())
206
+ .build();
207
+ ```
208
+
209
+ #### URL Provider (Optional)
210
+
211
+ By default, the URL is a static string. To use a different URL between connection attempts, provide
212
+ a function instead. The function is called on each connection attempt, including the initial one and
213
+ any retries. This enables use cases like load balancing, auth token rotation, and failover.
214
+
215
+ ```typescript
216
+ const ws = new WebsocketBuilder(() => `ws://localhost:42421?token=${getToken()}`)
217
+ .withBackoff(new ConstantBackoff(1000))
218
+ .build();
219
+ ```
220
+
221
+ ## Build & Tests
222
+
223
+ To compile the project, run `npm run build`.
224
+
225
+ To run tests, use `npm run test`.
@@ -43,7 +43,7 @@ var ExponentialBackoff = /** @class */ (function () {
43
43
  throw new Error("Base must be a positive integer or zero");
44
44
  }
45
45
  if (expMax !== undefined && (!Number.isInteger(expMax) || expMax < 0)) {
46
- throw new Error("ExpMax must be a undefined, a positive integer or zero");
46
+ throw new Error("ExpMax must be undefined, a positive integer, or zero");
47
47
  }
48
48
  this.base = base;
49
49
  this.expMax = expMax;
@@ -1 +1 @@
1
- {"version":3,"file":"exponentialbackoff.js","sourceRoot":"","sources":["../../../../src/backoff/exponentialbackoff.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH;IAME;;;;OAIG;IACH,4BAAY,IAAY,EAAE,MAAe;QAPjC,aAAQ,GAAW,CAAC,CAAC;QAQ3B,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC;YACxC,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;QAC7D,CAAC;QACD,IAAI,MAAM,KAAK,SAAS,IAAI,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC,CAAC,EAAE,CAAC;YACtE,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;QAC5E,CAAC;QAED,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;IACb,CAAC;IAED,sBAAI,uCAAO;aAAX;YACE,OAAO,IAAI,CAAC,QAAQ,CAAC;QACvB,CAAC;;;OAAA;IAED,sBAAI,uC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B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;IACb,CAAC;IAED,sBAAI,uCAAO;aAAX;YACE,OAAO,IAAI,CAAC,QAAQ,CAAC;QACvB,CAAC;;;OAAA;IAED,sBAAI,uCAAO;aAAX;YACE,OAAO,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;QACzC,CAAC;;;OAAA;IAED,iCAAI,GAAJ;QACE,IAAI,CAAC,QAAQ,EAAE,CAAC;QAChB,IAAI,CAAC,CAAC;YACJ,IAAI,CAAC,MAAM,KAAK,SAAS;gBACvB,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC;gBACZ,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QACxC,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,kCAAK,GAAL;QACE,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC;QAClB,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;IACb,CAAC;IACH,yBAAC;AAAD,CAAC,AA7CD,IA6CC;AA7CY,gDAAkB"}
@@ -5,7 +5,7 @@ export { LinearBackoff } from "./backoff/linearbackoff";
5
5
  export { Queue } from "./queue/queue";
6
6
  export { ArrayQueue } from "./queue/array_queue";
7
7
  export { RingQueue } from "./queue/ring_queue";
8
- export { Websocket } from "./websocket";
8
+ export { UrlProvider, Websocket } from "./websocket";
9
9
  export { WebsocketBuffer } from "./websocket_buffer";
10
10
  export { WebsocketBuilder } from "./websocket_builder";
11
11
  export { WebsocketEvent, RetryEventDetail, ReconnectEventDetail, WebsocketEventMap, WebsocketEventListener, WebsocketEventListenerParams, WebsocketEventListenerOptions, WebsocketEventListenerWithOptions, WebsocketEventListeners, } from "./websocket_event";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,oBAAoB,EACpB,iBAAiB,EACjB,sBAAsB,EACtB,4BAA4B,EAC5B,6BAA6B,EAC7B,iCAAiC,EACjC,uBAAuB,GACxB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,+BAA+B,EAAE,MAAM,2BAA2B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,oBAAoB,EACpB,iBAAiB,EACjB,sBAAsB,EACtB,4BAA4B,EAC5B,6BAA6B,EAC7B,iCAAiC,EACjC,uBAAuB,GACxB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,+BAA+B,EAAE,MAAM,2BAA2B,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":";;;AACA,6DAA4D;AAAnD,kHAAA,eAAe,OAAA;AACxB,mEAAkE;AAAzD,wHAAA,kBAAkB,OAAA;AAC3B,yDAAwD;AAA/C,8GAAA,aAAa,OAAA;AAEtB,mDAAiD;AAAxC,yGAAA,UAAU,OAAA;AACnB,iDAA+C;AAAtC,uGAAA,SAAS,OAAA;AAClB,yCAAwC;AAA/B,sGAAA,SAAS,OAAA;AAElB,yDAAuD;AAA9C,qHAAA,gBAAgB,OAAA;AACzB,qDAU2B;AATzB,iHAAA,cAAc,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":";;;AACA,6DAA4D;AAAnD,kHAAA,eAAe,OAAA;AACxB,mEAAkE;AAAzD,wHAAA,kBAAkB,OAAA;AAC3B,yDAAwD;AAA/C,8GAAA,aAAa,OAAA;AAEtB,mDAAiD;AAAxC,yGAAA,UAAU,OAAA;AACnB,iDAA+C;AAAtC,uGAAA,SAAS,OAAA;AAClB,yCAAqD;AAA/B,sGAAA,SAAS,OAAA;AAE/B,yDAAuD;AAA9C,qHAAA,gBAAgB,OAAA;AACzB,qDAU2B;AATzB,iHAAA,cAAc,OAAA"}
@@ -2,11 +2,17 @@ import { Backoff } from "./backoff/backoff";
2
2
  import { WebsocketBuffer } from "./websocket_buffer";
3
3
  import { WebsocketEvent, WebsocketEventListener, WebsocketEventListenerOptions } from "./websocket_event";
4
4
  import { WebsocketOptions } from "./websocket_options";
5
+ /**
6
+ * A URL or a function that returns a URL. When a function is provided, it is called on each connection attempt,
7
+ * enabling use cases like load balancing, auth token rotation, and failover.
8
+ */
9
+ export type UrlProvider = string | (() => string);
5
10
  /**
6
11
  * A websocket wrapper that can be configured to reconnect automatically and buffer messages when the websocket is not connected.
7
12
  */
8
13
  export declare class Websocket {
9
- private readonly _url;
14
+ private readonly _urlProvider;
15
+ private _url;
10
16
  private readonly _protocols?;
11
17
  private _closedByUser;
12
18
  private _lastConnection?;
@@ -16,11 +22,11 @@ export declare class Websocket {
16
22
  /**
17
23
  * Creates a new websocket.
18
24
  *
19
- * @param url to connect to.
25
+ * @param url to connect to, or a function that returns a URL.
20
26
  * @param protocols optional protocols to use.
21
27
  * @param options optional options to use.
22
28
  */
23
- constructor(url: string, protocols?: string | string[], options?: WebsocketOptions);
29
+ constructor(url: UrlProvider, protocols?: string | string[], options?: WebsocketOptions);
24
30
  /**
25
31
  * Getter for the url.
26
32
  *
@@ -1 +1 @@
1
- {"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../../../src/websocket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAGL,cAAc,EACd,sBAAsB,EACtB,6BAA6B,EAI9B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;GAEG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9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
19
19
  * Creates a new websocket.
20
20
  *
21
- * @param url to connect to.
21
+ * @param url to connect to, or a function that returns a URL.
22
22
  * @param protocols optional protocols to use.
23
23
  * @param options optional options to use.
24
24
  */
@@ -54,7 +54,7 @@ var Websocket = /** @class */ (function () {
54
54
  this.handleMessageEvent = function (event) {
55
55
  return _this.handleEvent(websocket_event_1.WebsocketEvent.message, event);
56
56
  };
57
- this._url = url;
57
+ this._urlProvider = url;
58
58
  this._protocols = protocols;
59
59
  // make a copy of the options to prevent the user from changing them
60
60
  this._options = {
@@ -305,7 +305,11 @@ var Websocket = /** @class */ (function () {
305
305
  * @return the created browser-native websocket which is also stored in the '_underlyingWebsocket' property.
306
306
  */
307
307
  Websocket.prototype.tryConnect = function () {
308
- this._underlyingWebsocket = new WebSocket(this.url, this.protocols); // create new browser-native websocket and add all event listeners
308
+ this._url =
309
+ typeof this._urlProvider === "function"
310
+ ? this._urlProvider()
311
+ : this._urlProvider;
312
+ this._underlyingWebsocket = new WebSocket(this._url, this.protocols); // create new browser-native websocket and add all event listeners
309
313
  this._underlyingWebsocket.addEventListener(websocket_event_1.WebsocketEvent.open, this.handleOpenEvent);
310
314
  this._underlyingWebsocket.addEventListener(websocket_event_1.WebsocketEvent.close, this.handleCloseEvent);
311
315
  this._underlyingWebsocket.addEventListener(websocket_event_1.WebsocketEvent.error, this.handleErrorEvent);