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

Multiple environments

Read time: 7 minutes
Last edited: May 02, 2024

Overview

This topic explains how to support multiple environments in LaunchDarkly mobile SDKs.

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.

Client-side SDKs

Some LaunchDarkly client-side mobile SDKs support having multiple LDClient instances tied to separate mobile keys. This allows the SDK to evaluate flags from multiple environments. The SDK can evaluate flags from different environments whether they are all in the same LaunchDarkly project or different LaunchDarkly projects.

However, other LaunchDarkly client-side mobile SDKs, including Android, iOS, and React Native, do not support multiple LDClient instances. Instead, you can configure the one SDK instance to connect to multiple environments.

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

  • Android
  • iOS
  • React Native

Android

Expand Android code sample

All LDClient instances evaluate against the same LDContext. The mobile keys for additional environments are specified, along with identifying names, in a map passed to your LDConfig object.

Here's how:

Map<String, String> otherKeys = new HashMap<String, String>();
otherKeys.put("platform", "platform-mobile-key-456def");
otherKeys.put("core", "core-mobile-key-789ghi");
LDConfig ldConfig = new LDConfig.Builder(AutoEnvAttributes.Enabled)
.mobileKey("mobile-key-123abc")
.secondaryMobileKeys(otherKeys)
.build();
LDContext context = LDContext.builder("context-key-123abc")
.set("email", "sandy@example.com")
.build();
LDClient.init(this.getApplication(), ldConfig, context);

To access the secondary mobile key instances, use the getForMobileKey method on LDClient. This method takes the identifier name assigned to your environment key in the secondaryMobileKeys map and returns the associated LDClient instance. Track calls, listeners, and flag evaluation are all tied to the client instance they are evaluated against.

Here's how:

LDClient coreInstance = LDClient.getForMobileKey("core");
// Variation determines whether or not a flag is enabled for a specific context
coreInstance.boolVariation("core-flag", false);
// allFlags produces a map of feature flag keys to their values
coreInstance.allFlags();
// track records actions end users take in your app
coreInstance.track("metric-key-123abc", data);

As all the client instances use the same LDClient object, some calls affect all instances.

These methods include:

LDClient coreInstance = LDClient.getForMobileKey("core");
// Calls affect all LDClient Instances
coreInstance.identify(/*Context Object*/);
coreInstance.flush();
coreInstance.setOffline();
coreInstance.setOnline();
coreInstance.close();

To learn more, read LDClient.

iOS

Expand iOS code sample

All LDClient instances evaluate against the same LDContext. The mobile keys for additional environments are specified, along with identifying names, in a map passed to your LDConfig object.

let context = try LDContextBuilder(key: "context-key-123abc").build().get()
var config = LDConfig(mobileKey: "mobile-key-123abc", autoEnvAttributes: .enabled)
// The SDK throws error strings if you add duplicate keys or put the primary key or name in secondaryMobileKeys.
try! config.setSecondaryMobileKeys(["platform": "platform-mobile-key-123abc", "core": "core-mobile-key-123abc"])
LDClient.start(config: config, context: context)

To access the secondary mobile key instances, use the LDClient.get static method on LDClient. This method takes the identifier name assigned to your environment key in the secondaryMobileKeys map and returns the associated LDClient instance. Track calls, listeners, and flag evaluation are all tied to the client instance they are evaluated against.

Here's how:

let coreInstance = LDClient.get(environment: "core")
// Variation determines whether or not a flag is enabled for a specific context
let coreFlagValue = coreInstance?.boolVariation(forKey: "core-flag-key-123abc", defaultValue: false)
// allFlags produces a map of feature flag keys to their values
let allFlags: [String: LDValue]? = coreInstance?.allFlags
// track records actions end users take in your app
try coreInstance?.track(key: "track-event-key-123abc", data: data)

As all the client instances use the same LDClient object, some SDK functionality affects all instances.

These methods include:

coreInstance.identify(/*Context Object*/)
coreInstance.flush()
coreInstance.setOnline(/*true or false*/)
coreInstance.close()

To learn more, read LDClient.

React Native

Expand React Native code sample

Starting with version 10 of the React Native SDK, if you need to support multiple environments in the same application, you can create multiple clients. We do not recommend using multiple environments within one application, because it quickly becomes complex and difficult to manage. If your setup requires it, here's how:

// not recommended: support multiple environments by creating multiple clients
const client1 = new ReactNativeLDClient('mobile-key-123abc', AutoEnvAttributes.Enabled);
const client2 = new ReactNativeLDClient('mobile-key-456def', AutoEnvAttributes.Enabled);
Expand for working with multiple environments in earlier versions

In previous versions of the React Native SDK, all LDClient instances evaluate against the same LDContext. The mobile keys for additional environments are specified, along with identifying names, in a map passed to your LDConfig object.

Here's how:

import LDClient, * as ld from 'launchdarkly-react-native-client-sdk';
let config: ld.LDConfig = {
mobileKey: 'mobile-key-123abc',
secondaryMobileKeys: {
'platform': 'platform-mobile-key-456def',
'core': 'core-mobile-key-789ghi'
},
};
let context: ld.LDContext = { 'key': 'user-key-123abc', 'kind': 'user' };
await client.configure(config, context);

To use functionality with a secondary environment, provide the name of the desired secondary environment as the environment parameter value to many LDClient methods. Some LDClient methods do not have an optional environment parameter because they act across all environments.

To use functionality with a secondary environment, provide the name of the desired secondary environment as the environment parameter value to many LDClient methods.

Here's how:

// Here, 'core' is the name of a secondary environment
// Variation determines whether or not a flag is enabled for a specific context
let boolResult = await client.boolVariation('bool-flag-key-123abc', false, 'core');
// allFlags produces a map of feature flag keys to their values
let allFlagsResult = client.allFlags('core');
allFlagsResult.then(values => {
console.log(values);
});
// track records actions end users take in your app
client.track('metric-flag-key-123abc', false, 'core');

Some LDClient methods do not have an optional environment parameter because they act across all environments.

These methods include:

identify(context);
flush();
setOnline();
setOffline();
isOffline():
close();