Multiple environments

Overview

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

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

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:

1Map<String, String> otherKeys = new HashMap<String, String>();
2otherKeys.put("platform", "platform-mobile-key-456def");
3otherKeys.put("core", "core-mobile-key-789ghi");
4
5LDConfig ldConfig = new LDConfig.Builder(AutoEnvAttributes.Enabled)
6 .mobileKey("mobile-key-123abc")
7 .secondaryMobileKeys(otherKeys)
8 .build();
9
10LDContext context = LDContext.builder("context-key-123abc")
11 .set("email", "sandy@example.com")
12 .build();
13
14LDClient.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:

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

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

These methods include:

1LDClient coreInstance = LDClient.getForMobileKey("core");
2
3// Calls affect all LDClient Instances
4coreInstance.identify(/*Context Object*/);
5coreInstance.flush();
6coreInstance.setOffline();
7coreInstance.setOnline();
8coreInstance.close();

To learn more, read LDClient.

iOS

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.

1let context = try LDContextBuilder(key: "context-key-123abc").build().get()
2var config = LDConfig(mobileKey: "mobile-key-123abc", autoEnvAttributes: .enabled)
3// The SDK throws error strings if you add duplicate keys or put the primary key or name in secondaryMobileKeys.
4try! config.setSecondaryMobileKeys(["platform": "platform-mobile-key-123abc", "core": "core-mobile-key-123abc"])
5LDClient.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:

1let coreInstance = LDClient.get(environment: "core")
2// Variation determines whether or not a flag is enabled for a specific context
3let coreFlagValue = coreInstance?.boolVariation(forKey: "core-flag-key-123abc", defaultValue: false)
4// allFlags produces a map of feature flag keys to their values
5let allFlags: [String: LDValue]? = coreInstance?.allFlags
6// track records actions end users take in your app
7try 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:

1coreInstance.identify(/*Context Object*/)
2coreInstance.flush()
3coreInstance.setOnline(/*true or false*/)
4coreInstance.close()

To learn more, read LDClient.

React Native

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:

React Native SDK, v10
1// not recommended: support multiple environments by creating multiple clients
2const client1 = new ReactNativeLDClient('mobile-key-123abc', AutoEnvAttributes.Enabled);
3const client2 = new ReactNativeLDClient('mobile-key-456def', AutoEnvAttributes.Enabled);

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:

1import LDClient, * as ld from 'launchdarkly-react-native-client-sdk';
2
3let config: ld.LDConfig = {
4 mobileKey: 'mobile-key-123abc',
5 secondaryMobileKeys: {
6 'platform': 'platform-mobile-key-456def',
7 'core': 'core-mobile-key-789ghi'
8 },
9};
10let context: ld.LDContext = { 'key': 'user-key-123abc', 'kind': 'user' };
11
12await 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:

React Native SDK v7.0 (JavaScript)
1// Here, 'core' is the name of a secondary environment
2
3// Variation determines whether or not a flag is enabled for a specific context
4let boolResult = await client.boolVariation('bool-flag-key-123abc', false, 'core');
5// allFlags produces a map of feature flag keys to their values
6let allFlagsResult = client.allFlags('core');
7allFlagsResult.then(values => {
8 console.log(values);
9});
10
11// track records actions end users take in your app
12client.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:

React Native SDK v7.0 (JavaScript)
1identify(context);
2flush();
3setOnline();
4setOffline();
5isOffline():
6close();
Built with