Flushing events

Overview

This topic explains how to use the flush feature. The flush feature is available in server-side, client-side, and edge SDKs.

Server-side, client-side, and edge SDKs automatically flush pending analytics events to LaunchDarkly at regular intervals. This prevents the SDK from having to send constant network requests. The time between intervals varies by SDK, and is configurable. To learn how to configure your SDK’s flush interval, read Configuration.

You can manually call flush to send events immediately without waiting for the next interval. Most customers do not need to use the flush feature because SDKs automatically flush their pending analytics events on a periodic frequency. However, it can be useful if you are using the SDK in a short-lived serverless process or a test application, rather than in a long-running application. To learn more, read Analytics events.

About the flush feature

The flush feature tells the client to send all of an SDK’s pending analytics events to LaunchDarkly as soon as possible.

All SDKs support asynchronous flushing, which tells the SDK to start delivering events, but returns control to the application before delivery is complete. Some SDKs also support synchronous flushing, which tells the SDK to deliver the events and not return control until delivery is complete.

Details about each SDK’s configuration are available in the SDK-specific sections below.

Client-side SDKs

This feature is available in the following client-side SDKs:

.NET (client-side)

Internally, the client-side .NET SDK keeps an event buffer for Track and Identify calls. These are flushed periodically in a background thread. If you test the SDK in a REPL, you may want to manually call Flush to process events immediately.

The flush interval is configurable. If you need to change the interval, you can do so with the Configuration class.

To call flush:

C#
1client.Flush();

Android

The Android SDK keeps an internal event buffer for analytics calls. These are flushed periodically in a background thread. You can configure the flush interval if needed.

In some situations, such as when you’re testing the SDK in a simulator, you may want to manually call flush to request any queued events to be sent immediately. This call is non-blocking, so it returns before the events are sent.

Here’s how:

1client.flush();

The flush interval is configurable.

C++ (client-side)

The LaunchDarkly SDK keeps an internal event buffer for analytics events. These are flushed periodically in a background thread. In some situations, for example if you’re testing the SDK in a simulator, you may want to manually call flush to process events immediately.

1client.FlushAsync();

You can also examine the result to determine if the flush succeeded. Here’s how:

1auto flush_result = client.FlushAsync(context);
2auto status = flush_result.wait_for(maxwait);
3
4if (status == std::future_status::ready) {
5 /* The client's attempt to flush succeeded or failed in the specified amount of time. */
6 if (flush_result.get()) {
7 /* Flush succeeded */
8 } else {
9 /* Flush failed */
10 }
11} else {
12 /* The specified timeout was reached, but the client is still attempting to flush. */
13}

The flush interval is configurable. If you need to change the interval, you can do so using the configuration. To learn more, read FlushAsync and FlushInterval.

Flutter

Internally, the Flutter SDK keeps an event buffer for track calls. These are flushed periodically in a background thread. You can configure the flush interval if needed.

In some situations, such as when you’re testing the SDK in a simulator, you may want to manually call flush to request any queued events to be sent immediately. This call is non-blocking, so it returns before the events are sent.

To call flush:

Dart
1await client.flush();

To learn more, read flush.

iOS

Internally, the iOS SDK keeps an event buffer for track calls. These are flushed periodically in a background thread. You can configure the flush interval if needed.

In some situations, such as when you’re testing the SDK in a simulator, you may want to manually call flush to request any queued events to be sent immediately. This call is non-blocking, so it returns before the events are sent.

To call flush:

1LDClient.get()!.flush()

JavaScript

Internally, the JavaScript SDK keeps an event buffer for track calls. These are flushed periodically in a background thread. You can configure the flush interval if needed.

In some situations, such as when you’re testing the SDK in a simulator, you may want to manually call flush to request any queued events to be sent immediately. This call is non-blocking, so it returns before the events are sent.

This method is asynchronous. You can pass a callback or wait for the returned Promise to determine when all events have been flushed.

To call flush:

JavaScript
1client.flush();

Node.js (client-side)

Internally, the LaunchDarkly SDK keeps an analytics event buffer. These events are flushed periodically. In some situations, you may want to manually call flush to process events immediately.

This method is asynchronous. You can pass a callback or wait for the returned Promise to determine when all events have been flushed.

To call flush:

JavaScript
1client.flush();
2
3// or, with a callback:
4client.flush(() => {
5 console.log('flush complete');
6});
7
8// or, with a Promise:
9client.flush().then(() => {
10 console.log('flush complete');
11});

React Native

Internally, the React Native SDK keeps an event buffer for track calls. These are flushed periodically in a background thread. You can configure the flush interval if needed.

In some situations, such as when you’re testing the SDK in a simulator, you may want to manually call flush to request any queued events to be sent immediately.

To call flush:

1const { result, error } = await client.flush();

flush is asynchronous and can be awaited. It returns a promise that resolves to an object containing an error, if there is one, and a boolean result.

To learn more, read flush.

Roku

Internally, the Roku SDK keeps an event buffer for track calls. These are flushed periodically in a background thread. You can configure the flush interval if needed.

In some situations, such as when you’re testing the SDK in a simulator, you may want to manually call flush to request any queued events to be sent immediately. This call is non-blocking, so it returns before the events are sent.

To call flush:

BrightScript
1launchDarkly.flush()

Server-side SDKs

This feature is available in the following server-side SDKs:

.NET (server-side)

The .NET (server-side) SDK supports asynchronous flushing with the Flush method.

To call flush:

C#
1client.Flush();

Starting in version 7.0.0, the SDK also supports synchronous flushing with the FlushAndWait method. In this example, the TimeSpan.FromSeconds(2) value means that the application is willing to wait no more than two seconds for event delivery.

C#
1client.FlushAndWait(TimeSpan.FromSeconds(2));

The flush interval is configurable. If you need to change the interval, you can do so with the Configuration class.

Here’s how:

C#
1var config = Configuration.Builder("sdk-key-123abc")
2 .Events(
3 Components.SendEvents().FlushInterval(TimeSpan.FromSeconds(10))
4 )
5 .Build();
6var client = new LdClient(config);

C++ (server-side)

The LaunchDarkly SDK keeps an internal event buffer for analytics events. These are flushed periodically in a background thread. If you test the SDK in a simulator, you may want to manually call flush to process events immediately.

This function will not block, but instead initiate a flush operation in the background. The flush interval is configurable. If you need to change the interval, you can do so with the configuration.

Here’s how:

1client.FlushAsync();

The flush interval is configurable. If you need to change the interval, you can do so using the configuration. To learn more, read about FlushAsync() in Client and about FlushInterval() in EventsBuilder.

Go

The Go SDK supports asynchronous flushing with the Flush method.

Go
1client.Flush();

Starting in version 6.0.0, the SDK also supports synchronous flushing with the FlushAndWait method. In this example, the time.Second*2 value means that the application is willing to wait no more than two seconds for event delivery.

Go
1client.FlushAndWait(time.Second*2);

The interval for automatic event flushing is configurable. If you need to change the interval, you can do so by making a custom client configuration. Here’s how:

Go
1config := ld.Config{
2 Events: ldcomponents.SendEvents().FlushInterval(time.Second*10),
3}

Haskell

The LaunchDarkly SDK keeps an internal event buffer for analytics events. These events are flushed periodically in a background thread. If you test the SDK in a simulator, you may want to manually call flush to process events immediately.

This function will not block, but instead initiate a flush operation in the background. The flush interval is configurable. If you need to change the interval, you can do so with the configuration.

Here’s how:

Haskell
1flushEvents client

Java

Internally, the LaunchDarkly SDK keeps an event buffer for track and identify calls. These are flushed periodically in a background thread. If you test the SDK in a REPL, you may want to manually call flush to process events immediately.

Here’s how:

Java
1client.flush();

The flush interval is configurable. If you need to change the interval, you can do so with LDConfig.Builder and Components.sendEvents().

Lua

The LaunchDarkly SDK keeps an internal event buffer for analytics events. These are flushed periodically in a background thread. If you test the SDK in a simulator, you may want to manually call flush to process events immediately.

This function will not block, but instead initiate a flush operation in the background. The flush interval is configurable. If you need to change the interval, you can do so with the configuration.

Here’s how:

Lua
1client:flush()

To learn more, read flush.

Node.js (server-side)

Internally, the LaunchDarkly SDK keeps an event buffer for track and identify calls. These are flushed periodically in a background thread. If you test the SDK in a REPL, you may want to manually call flush to process events immediately.

The flush interval is configurable. If you need to change the interval, you can do so when configuring your client instance.

Here’s how:

JavaScript
1client.flush();

PHP

Internally, the LaunchDarkly SDK keeps an event buffer for variation, track, and identify calls. These are automatically flushed when the LDClient is destroyed. PHP’s shared-nothing architecture means manual invocation of this method is typically not needed. Developers may do so if they wish to flush events prior to teardown.

Here’s how:

PHP
1ldclient->flush();

Python

Internally, the LaunchDarkly SDK keeps an event buffer for variation, track, and identify calls. These are flushed periodically in a background thread. If you test the SDK in a REPL, you may want to manually call flush to process events immediately. Otherwise, Python may close before flushing the event buffer and your user changes and tracks will be lost.

The flush interval is configurable. If you need to change the interval, you can do so when you configure your client instance.

Here’s how:

Python
1ldclient.get().flush()

Ruby

Internally, the LaunchDarkly SDK keeps an event buffer for track and identify calls. These are flushed periodically in a background thread. If you test the SDK in a REPL, you may want to manually call flush to process events immediately.

The flush interval is configurable. If you need to change the interval, you can do so when you configure your client instance.

Here’s how:

Ruby
1client.flush

Rust

Internally, the LaunchDarkly SDK keeps an event buffer for the analytics events that are produced by calling the variation or variation_detail methods, the track methods, or identify. These are flushed periodically in a background thread. In some situations, you may want to manually call flush to process events immediately.

The flush interval is configurable. If you need to change the interval, you can do so by making a custom client configuration.

Here’s how:

Rust
1let result = client.flush();

Edge SDKs

Some edge SDKs support sending events directly. When you configure an edge SDK to send events, you must also flush those events to ensure that they are sent back to LaunchDarkly.

This feature is available in the following edge SDKs:

Cloudflare

Flushing events is available in Cloudflare SDK version 2.3.0 and later.

If you send events, you must also flush those events before your worker exits to ensure that they are sent back to LaunchDarkly.

If you call flush inside the waitUntil method, then flushing events will not impact the handler’s response time. To learn more, read the Cloudflare documentation on waitUntil.

Here’s how:

Cloudflare SDK v2.3.0+ (TypeScript)
1// executionContext is the Cloudflare worker handler context
2// https://github.com/cloudflare/workers-types/blob/master/index.d.ts#L567
3executionContext.waitUntil(
4 client.flush((err, res) => {
5 console.log(`flushed events result: ${res}, error: ${err}`);
6 }),
7);

If you do not call flush, the events will not be sent to LaunchDarkly servers, due to the ephemeral nature of edge workers.

Vercel

Flushing events is available in Vercel SDK v1.2.0 and later.

If you send events, you must also flush those events before your worker exits to ensure that they are sent back to LaunchDarkly.

If you call flush inside the waitUntil method, then flushing events will not impact the handler’s response time. To learn more, read the Vercel documentation on waitUntil.

Here’s how:

Vercel SDK v1.2.0+ (TypeScript)
1context.waitUntil(
2 client.flush((err, res) => {
3 console.log(`flushed events result: ${res}, error: ${err}`);
4 }),
5);

If you do not call flush, the events will not be sent to LaunchDarkly servers, due to the ephemeral nature of edge workers.

Built with