Offline mode
Read time: 11 minutes
Last edited: Sep 25, 2024
Overview
This topic explains how to set an SDK to offline mode. Offline mode closes an SDK's connection to LaunchDarkly and switches to a feature flag's last known values.
A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: "users." To learn more, read Contexts.
Creating contexts and evaluating flags based on them is supported in the latest major versions of most of our SDKs. For these SDKs, the code samples on this page include the two most recent versions.
Fallback values and client-side SDKs
For client-side SDKs, if LaunchDarkly is unreachable or if an end user’s device is not connected to a network, such as when the device is in airplane mode, the SDK uses the latest stored flag variation values in memory. If there are no previously stored variation values, the SDK uses the fallback values that you define in your code.
In some situations, you might want to avoid remote calls to LaunchDarkly and use fallback values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on-premises, it might make sense to use fallback values when running on-premises. You can do this by setting offline mode in the client's config object. When the client is in offline mode, it makes no network requests, so it is suitable for unit-testing.
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)
Expand .NET (client-side) code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
Here's how:
Android
Expand Android code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
Here's how:
C++ (client-side)
Expand C++ (client-side) code sample
In some situations, you might want to stop making remote calls to LaunchDarkly. You can set the SDK offline, and no data will come into or out of the SDK. All flags will return the fallback values defined in your code.
Here's how:
Flutter
Expand Flutter code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
Here's how:
To learn more, read offline
.
iOS
Expand iOS code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
The SDK protects itself from multiple rapid calls to setOnline(true)
by enforcing an increasing delay (called throttling) each time setOnline(true)
is called within a short time. The first time, the call proceeds normally. For each subsequent call, the delay is enforced, and if waiting, increased to a maximum delay. When the delay has elapsed, the setOnline(true)
proceeds, assuming that the client app has not called setOnline(false)
during the delay. Therefore, a call to setOnline(true)
may not immediately result in the LDClient going online.
Client app developers should consider this situation abnormal, and take steps to prevent the client app from making multiple rapid setOnline(true)
calls. Calls to setOnline(false)
are not throttled. Calls to start(config: context: completion:)
, and setting the config
or context
can also call setOnline(true)
under certain conditions. After the delay, the SDK resets and the client app can make a subsequent call to setOnline(true)
without being throttled.
Client apps can set a completion closure called when the setOnline call completes. For unthrottled setOnline(true)
and all setOnline(false)
calls, the SDK calls the closure immediately on completion of this method. For throttled setOnline(true)
calls, the SDK calls the closure after the throttling delay at the completion of the setOnline
method.
The SDK does not go online if the client has not been started, or the mobileKey
is empty. For macOS, the SDK does not go online in the background unless enableBackgroundUpdates
is true.
Use isOnline
to get the online/offline state.
Here is an example:
React Native
Expand React Native code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
Use setConnectionMode
to do this. The following connection modes are supported:
offline
. In this connection mode, the SDK stops receiving updates and stops sending analytic and diagnostic events.streaming
. In this connection mode, the SDK uses a streaming connection to receive updates from LaunchDarkly. This is the default.
Here's how:
To learn more, read setConnectionMode
.
Server-side SDKs
This feature is available in the following server-side SDKs:
- .NET (server-side)
- C++ (server-side)
- Erlang
- Go
- Haskell
- Java
- Lua
- Node.js (server-side)
- PHP
- Python
- Ruby
- Rust
.NET (server-side)
Expand .NET (server-side) code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
Here's how:
C++ (server-side)
Expand C++ (server-side) code sample
In some situations, you might want to stop making remote calls to LaunchDarkly. You can set the SDK offline, and no data will come into or out of the SDK. All flags will return the fallback values defined in your code.
Offline mode in the C++ server-side SDK is a shorthand for manually disabling events (config_builder.Events().Disable()
) and the SDK's Data System (config_builder.DataSystem().Disable()
).
Here's how:
Erlang
Expand Erlang code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
Here's how:
Go
Expand Go code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
Here's how:
Haskell
Expand Haskell code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
Here's how:
Java
Expand Java code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
Here's how:
Lua
Expand Lua code sample
In some situations, you might want to stop making remote calls to LaunchDarkly. You can set the SDK offline, and no data will come into or out of the SDK. All flags will return the fallback values defined in your code.
Here's how:
Node.js (server-side)
Expand Node.js (server-side) code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
Here's how:
The default value you set in the variation method is returned in offline mode. This does not refer to the default rule set in your flag configuration.
PHP
Expand PHP code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
Here's how:
Python
Expand Python code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
Here's how:
Ruby
Expand Ruby code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
Here's how:
Rust
Expand Rust code sample
In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached flag values for future evaluations.
Here's how: