• HOME
  • INTEGRATIONS
  • SDKS
  • GUIDES
  • API DOCS
No results for ""
EXPAND ALL
launchdarkly.com

EDIT ON GITHUB

Offline mode

Read time: 5 minutes
Last edited: Jun 18, 2021

Overview

This topic explains how to set an SDK to offline mode.

Understanding offline mode

Offline mode closes an SDK's connection to LaunchDarkly and switches to a feature flag's last known values.

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

  • Client-side SDKs
  • Server-side SDKs

Client-side SDKs

If a 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 settings in memory. If there are no previously stored flag settings, the SDK uses the default values.

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

Android

In some situations, you might want to stop making remote calls to LaunchDarkly and switch to the last known values for your feature flags. offline mode lets you do this easily.

1LDConfig ldConfig = new LDConfig.Builder()
2 .setMobileKey("YOUR_MOBILE_KEY")
3 .setOffline(true)
4 .build();
5
6LDClient ldClient = LDClient.init(this.getApplication(), ldConfig, user);
7
8// Or to switch an already-instantiated client to offline mode:
9ldClient.setOffline();

Flutter

In some situations, you might want to stop making remote calls to LaunchDarkly and rely on locally cached values for your feature flags for future evaluations. offline mode lets you do this easily.

1LDConfig ldConfig = LDConfigBuilder('YOUR_MOBILE_KEY')
2 .setOffline(true)
3 .build();
4
5await LDClient.start(ldConfig, user);
6
7// Or to switch an already-instantiated client to offline mode:
8await LDClient.setOnline(false);

iOS

In some situations, you might want to stop making remote calls to LaunchDarkly and switch to the last known values for your feature flags. Offline mode lets you do this easily.

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: user: completion:), and setting the config or user 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.

1LDClient.get()!.setOnline(false)
2LDClient.get()!.setOnline(true) {
3 // Client is online
4}
5let connectionStatus = LDClient.get()!.isOnline

React Native

In some situations, you might want to stop making remote calls to LaunchDarkly and switch to the last known values for your feature flags. Offline mode lets you do this easily.

1let offlineResult = client.setOffline(); //true or false
2let offlineStatus = client.isOffline(); //true or false
3let onlineResult = client.setOnline(); //true or false

Xamarin

The default (fallback) values are defined in your code. The default value only returns if an error is encountered or if LaunchDarkly is unreachable.

In some situations, you might want avoid remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on-premise, it might make sense to fall back to defaults when running on-premise. You can do this by setting offline mode in the client's Config. When the client is in offline mode, no network requests will be made, so it is suitable for unit-testing.

1Configuration config = Configuration.Builder("SDK_KEY")
2 .Offline(true)
3 .Build();
4LdClient client = LdClient.Init(config);

Server-side SDKs

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

.NET

The default (fallback) values are defined in your code. The default value only returns if an error is encountered or if LaunchDarkly is unreachable.

In some situations, you might want avoid remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on-premise, it might make sense to fall back to defaults when running on-premise. You can do this by setting offline mode in the client's Config. When the client is in offline mode, no network requests will be made, so it is suitable for unit-testing.

1Configuration config = Configuration.Builder("SDK_KEY")
2 .Offline(true)
3 .Build();
4LdClient client = new LdClient(config);

C/C++ (server-side)

In some situations, you might want to stop making remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on-premise, it might make sense to fall back to defaults when running on-premise.

1LDConfigSetOffline(config, true);

Erlang

In some situations, you might want to stop making remote calls to LaunchDarkly and fall back to your feature flags' default values.

For example, if your software is both cloud-hosted and distributed to customers to run on-premises, it might make sense to fall back to defaults when the software runs on-premises.

You can do this by setting offline mode in the config map with the offline key.

ldclient:start_instance("YOUR_SDK_KEY", #{offline => true})

Go

In some situations, you might want to stop making remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on-premise, it might make sense to fall back to defaults when running on-premise. You can do this by setting Offline mode in the client's Config.

1var config ld.Config
2config.Offline = true
3
4ldClient, _ := ld.MakeCustomClient("YOUR_SDK_KEY", config, 5 * time.Second)
5ldClient.BoolVariation("any.feature.flag", user, false) // will always return the default value (false)

Haskell

In some situations, you might want to stop making remote calls to LaunchDarkly and fall back to default values for your feature flags. You can do this by setting offline mode in the config object with configSetOffline.

For example, if your software is both cloud-hosted and distributed to customers to run on-premise, it might make sense to fall back to defaults when running on-premise.

1config' = configSetOffline True config

Java

In some situations, you might want to stop making remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on-premise, it might make sense to fall back to defaults when running on-premise. You can do this by setting offline mode in the client's LDConfig.

1LDConfig config = new LDConfig.Builder()
2 .offline(true)
3 .build();
4LDClient ldClient = new LDClient("YOUR_SDK_KEY", config);
5ldClient.boolVariation("any.feature.flag", user, false)
6
7// This call to ldClient.boolVariation always
8// returns the default value (false)

Node.js (server-side)

In some situations, you might want to stop making remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on-premise, it might make sense to fall back to defaults when running on-premise. You can do this by setting offline mode in the client's config object.

1var config = {"offline": true};
2ldClient = LaunchDarkly.init("YOUR_SDK_KEY", config);
3ldClient.variation("any.feature.flag", user, false, cb) // cb will always be invoked with the default value (false)

Note that the default value you set in the variation method will be returned in offline mode, this does not refer to the default rule set in your flag configuration.

PHP

In some situations, you might want to stop making remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on-premise, it might make sense to fall back to defaults when running on-premise. setOffline lets you do this easily.

1$client = new LaunchDarkly\LDClient("YOUR_SDK_KEY", array("offline" => true));
2$client->variation("any.feature.flag", user, false); // will always return the default value (false)

Python

In some situations, you might want avoid remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on-premise, it might make sense to fall back to defaults when running on-premise. You can do this by setting offline mode in the client's Config.

1# Initialization:
2ldclient.set_config(Config("YOUR_SDK_KEY", offline=True))
3ldclient.get().variation("any.feature.flag", user, False) # will always return the default value (false)

Ruby

In some situations, you might want to stop making remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on-premise, it might make sense to fall back to defaults when running on-premise. Offline mode lets you do this easily.

1config = LaunchDarkly::Config.new({offline: true})
2ld_client = LaunchDarkly::LDClient.new("YOUR_SDK_KEY", config)
3ld_client.variation("any.feature.flag", user, false) # will always return the default value (false)