No results for ""
EXPAND ALL
  • Home
  • API docs

Offline mode

Read time: 11 minutes
Last edited: May 02, 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.

Newer versions of LaunchDarkly SDKs replace users with contexts

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 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. The fallback values are defined in your code. The SDK returns the fallback value if it encounters an error or if LaunchDarkly is unreachable.

In some situations, you might want 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
  • Server-side SDKs

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:

var config = Configuration
.Builder("mobile-key-123abc", ConfigurationBuilder.AutoEnvAttributes.Enabled)
.Offline(true)
.Build();
LdClient client = LdClient.Init(config);

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:

LDConfig ldConfig = new LDConfig.Builder(AutoEnvAttributes.Enabled)
.mobileKey("mobile-key-123abc")
.setOffline(true)
.build();
LDClient client = LDClient.init(this.getApplication(), ldConfig, context);
// Or to switch an already-instantiated client to offline mode:
client.setOffline();

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:

config_builder.Offline(true);

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:

final config = LDConfig(
CredentialSource.fromEnvironment,
AutoEnvAttributes.enabled,
dataSourceConfig: DataSourceConfig(
initialConnectionMode: ConnectionMode.streaming // or .polling, or .offline
),
);
await client.start(config, context);
// To switch an already-instantiated client to offline mode:
client.offline = true;
// To switch it back:
client.offline = false;

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:

LDClient.get()!.setOnline(false)
LDClient.get()!.setOnline(true) {
// Client is online
}
let connectionStatus = LDClient.get()!.isOnline

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:

await client.setConnectionMode('offline');
await client.setConnectionMode('streaming');

To learn more, read setConnectionMode.

Server-side SDKs

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

.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:

var config = Configuration.Builder("sdk-key-123abc")
.Offline(true)
.Build();
LdClient client = new LdClient(config);

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:

config_builder.Offline(true);

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:

ldclient:start_instance("sdk-key-123abc", #{offline => true})

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:

var config ld.Config
config.Offline = true
client, _ := ld.MakeCustomClient("sdk-key-123abc", config, 5*time.Second)
client.BoolVariation("any.feature.flag", context, false) // will always return the default value (false)

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:

config' = configSetOffline True config

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:

LDConfig config = new LDConfig.Builder()
.offline(true)
.build();
LDClient client = new LDClient("sdk-key-123abc", config);
client.boolVariation("flag-key-123abc", context, false)
// This call to client.boolVariation always
// returns the default value (false)

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:

local config = {
offline = true
}

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:

import * as ld from '@launchdarkly/node-server-sdk';
const options: ld.LDOptions = { offline: true };
const client = ld.init('sdk-key-123abc', options);
client.variation('any.feature.flag', context, false, cb); // cb will always be invoked with the default value (false)

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:

$client = new LaunchDarkly\LDClient("sdk-key-123abc", ["offline" => true]);
$client->variation("any.feature.flag", $context, false); // will always return the default value (false)

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:

# Initialization:
ldclient.set_config(Config("sdk-key-123abc", offline=True))
ldclient.get().variation("any.feature.flag", context, False) # will always return the default value (false)

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:

config = LaunchDarkly::Config.new({offline: true})
client = LaunchDarkly::LDClient.new("sdk-key-123abc", config)
client.variation("any.feature.flag", context, false) # will always return the default value (false)

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:

let config = ConfigBuilder::new("sdk-key-123abc").offline(true).build();
let ld_client = Client::build(config).unwrap();
ld_client.bool_variation(&context, "flag-key-123abc", false); // will always return the default value (false)