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
To use the Bucket NodeSDK with Cloudflare workers, set the `node_compat` flag [in your wrangler file](https://developers.cloudflare.com/workers/runtime-apis/nodejs/#get-started).
213
+
214
+
Instead of using `BucketClient`, use `EdgeClient` and make sure you call `ctx.waitUntil(bucket.flush());` before returning from your worker function.
215
+
216
+
```typescript
217
+
import { EdgeClient } from"@bucketco/node-sdk";
218
+
219
+
// set the BUCKET_SECRET_KEY environment variable or pass the secret key in the constructor
// initialize the client and wait for it to complete
225
+
// if the client was initialized on a previous invocation, this is a no-op.
226
+
awaitbucket.initialize();
227
+
const features =bucket.getFeatures({
228
+
user: { id: "userId" },
229
+
company: { id: "companyId" },
230
+
});
231
+
232
+
// ensure all events are flushed and any requests to refresh the feature cache
233
+
// have completed after the response is sent
234
+
ctx.waitUntil(bucket.flush());
235
+
236
+
returnnewResponse(
237
+
`Features for user ${userId} and company ${companyId}: ${JSON.stringify(features, null, 2)}`,
238
+
);
239
+
},
240
+
};
241
+
```
242
+
243
+
See [examples/cloudflare-worker](examples/cloudflare-worker/src/index.ts) for a deployable example.
244
+
245
+
Bucket maintains a cached set of feature definitions in the memory of your worker which it uses to decide which features to turn on for which users/companies.
246
+
247
+
The SDK caches feature definitions in memory for fast performance. The first request to a new worker instance fetches definitions from Bucket's servers, while subsequent requests use the cache. When the cache expires, it's updated in the background. `ctx.waitUntil(bucket.flush())` ensures completion of the background work, so response times are not affected. This background work may increase wall-clock time for your worker, but it will not measurably increase billable CPU time on platforms like Cloudflare.
248
+
210
249
## Error Handling
211
250
212
251
The SDK is designed to fail gracefully and never throw exceptions to the caller. Instead, it logs errors and provides
@@ -636,11 +675,8 @@ these functions.
636
675
637
676
## Flushing
638
677
639
-
It is highly recommended that users of this SDK manually call `flush()`
640
-
method on process shutdown. The SDK employs a batching technique to minimize
641
-
the number of calls that are sent to Bucket's servers. During process shutdown,
642
-
some messages could be waiting to be sent, and thus, would be discarded if the
643
-
buffer is not flushed.
678
+
BucketClient employs a batching technique to minimize the number of calls that are sent to
679
+
Bucket's servers.
644
680
645
681
By default, the SDK automatically subscribes to process exit signals and attempts to flush
646
682
any pending events. This behavior is controlled by the `flushOnExit` option in the client configuration:
@@ -653,17 +689,6 @@ const client = new BucketClient({
653
689
});
654
690
```
655
691
656
-
> [!NOTE]
657
-
> If you are creating multiple client instances in your application, it's recommended to disable `flushOnExit`
658
-
> to avoid potential conflicts during process shutdown. In such cases, you should implement your own flush handling.
659
-
660
-
When you bind a client to a user/company, this data is matched against the
661
-
targeting rules. To get accurate targeting, you must ensure that the user/company
662
-
information provided is sufficient to match against the targeting rules you've
663
-
created. The user/company data is automatically transferred to Bucket. This ensures
664
-
that you'll have up-to-date information about companies and users and accurate
665
-
targeting information available in Bucket at all time.
666
-
667
692
## Tracking custom events and setting custom attributes
668
693
669
694
Tracking allows events and updating user/company attributes in Bucket.
0 commit comments