@flipt-io/flipt-client-js 0.5.1 → 0.6.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.
package/README.md CHANGED
@@ -11,6 +11,9 @@ The `flipt-client-js` library contains the JavaScript/TypeScript client for Flip
11
11
 
12
12
  ## Installation
13
13
 
14
+ > [!NOTE]
15
+ > The Node.js build requires Node.js 20 or later.
16
+
14
17
  ```bash
15
18
  npm install @flipt-io/flipt-client-js
16
19
  ```
@@ -62,6 +65,8 @@ The `FliptClient` constructor accepts the following optional arguments:
62
65
  - `namespace`: The namespace to fetch flag state from. If not provided, the client will default to the `default` namespace.
63
66
  - `url`: The URL of the upstream Flipt instance. If not provided, the client will default to `http://localhost:8080`.
64
67
  - `updateInterval`: **Node.js Only** The interval (in seconds) in which to fetch new flag state. If not provided, the client will default to 120 seconds.
68
+ - `fetchMode`: How the client receives flag state updates: `FetchMode.Polling` (default) or `FetchMode.Streaming`. See [Streaming](#streaming-server-sent-events).
69
+ - `logger`: An optional logger (`debug`, `info`, `warn`, `error`) for diagnostics such as streaming events. Logging is disabled by default.
65
70
  - `authentication`: The authentication strategy to use when communicating with the upstream Flipt instance. If not provided, the client will default to no authentication. See the [Authentication](#authentication) section for more information.
66
71
  - `reference`: The [reference](https://docs.flipt.io/guides/user/using-references) to use when fetching flag state. If not provided, reference will not be used.
67
72
  - `snapshot`: A base64-encoded snapshot returned by `client.getSnapshot()`. When provided with `errorStrategy: ErrorStrategy.Fallback`, the client can initialize while offline and refresh from Flipt when the network becomes available.
@@ -215,12 +220,35 @@ const client = await FliptClient.init({
215
220
  });
216
221
  ```
217
222
 
218
- Make sure to call the `close` method on the `FliptClient` class once you are done using it to stop the timer and clean up resources.
223
+ Make sure to call the `close` method on the `FliptClient` class once you are done using it to stop the timer (or close the stream) and clean up resources.
219
224
 
220
225
  ```typescript
221
226
  client.close();
222
227
  ```
223
228
 
229
+ ### Streaming (Server-Sent Events)
230
+
231
+ Instead of polling, the client can receive flag state updates in real time over [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events). The client refetches the flag state whenever the server signals a change, and again each time the stream reconnects so no updates are missed.
232
+
233
+ ```typescript
234
+ import { FliptClient, FetchMode } from '@flipt-io/flipt-client-js';
235
+
236
+ const client = await FliptClient.init({
237
+ url: 'http://localhost:8080',
238
+ fetchMode: FetchMode.Streaming,
239
+ logger: console // optional
240
+ });
241
+ ```
242
+
243
+ > [!NOTE]
244
+ >
245
+ > - In streaming mode `updateInterval` is ignored and no polling timer is started.
246
+ > - **Browser:** the native `EventSource` cannot send custom headers, so `authentication` is not applied to the stream. Use cookie-based auth or a Flipt server that does not require authentication for the stream endpoint.
247
+ > - **Node.js:** streaming uses the [`eventsource`](https://www.npmjs.com/package/eventsource) package. Authentication headers are sent with the stream request.
248
+ > - `EventSource` reconnects automatically after transient errors. If the connection is closed permanently, the client logs an error (via `logger`) and keeps serving the last known flag state.
249
+
250
+ Call `close()` when you are done with the client to close the stream.
251
+
224
252
  ## ETag Support
225
253
 
226
254
  The default fetcher uses [ETag HTTP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) to reduce the number of requests to the Flipt instance. This is useful in scenarios where the flag state is not frequently updated and you want to reduce the load on the server.