Flushing events
Read time: 12 minutes
Last edited: May 30, 2024
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)
- Android
- C++ (client-side)
- Flutter
- iOS
- JavaScript
- Node.js (client-side)
- React Native
- Roku
.NET (client-side)
Expand .NET (client-side) code sample
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
:
Android
Expand Android code sample
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:
The flush interval is configurable.
C++ (client-side)
Expand C++ (client-side) code sample
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.
You can also examine the result to determine if the flush succeeded. Here's how:
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
Expand Flutter code sample
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
:
To learn more, read flush
.
iOS
Expand iOS code sample
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
:
JavaScript
Expand JavaScript code sample
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
:
Node.js (client-side)
Expand Node.js (client-side) code sample
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
:
React Native
Expand React Native code sample
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
:
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
Expand Roku code sample
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
:
Server-side SDKs
This feature is available in the following server-side SDKs:
.NET (server-side)
Expand .NET (server-side) code sample
The .NET (server-side) SDK supports asynchronous flushing with the Flush
method.
To call 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.
The flush interval is configurable. If you need to change the interval, you can do so with the Configuration
class.
Here's how:
C++ (server-side)
Expand C++ (server-side) code sample
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:
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
Expand Go code sample
The Go SDK supports asynchronous flushing with the Flush
method.
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.
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:
Haskell
Expand Haskell code sample
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:
Java
Expand Java code sample
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:
The flush interval is configurable. If you need to change the interval, you can do so with LDConfig.Builder
and Components.sendEvents()
.
Lua
Expand Lua code sample
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:
To learn more, read flush
.
Node.js (server-side)
Expand Node.js (server-side) code sample
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:
PHP
Expand PHP code sample
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:
Python
Expand Python code sample
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:
Ruby
Expand Ruby code sample
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:
Rust
Expand Rust code sample
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:
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
Expand Cloudflare code sample
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:
If you do not call flush
, the events will not be sent to LaunchDarkly servers, due to the ephemeral nature of edge workers.
Vercel
Expand Vercel code sample
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:
If you do not call flush
, the events will not be sent to LaunchDarkly servers, due to the ephemeral nature of edge workers.