LaunchDarkly Developer Documentation

Get started in under 30 minutes.
LaunchDarkly provides feature flags as a service for Java · Python · Ruby · Go · Node.js · PHP · .NET. Control feature launches -- who sees what and when -- without multiple code deploys. Easy dashboard for phased rollouts, targeting and segmenting.
Need more help? write us at support@launchdarkly.com

Get Started    Guides

Electron SDK Reference

This is BETA software

The Electron SDK should not be used in production environments until a final version is released.

This reference guide documents the LaunchDarkly SDK for the Electron desktop application framework. This is a variant of the client-side JavaScript SDK with additional functionality for Electron.

Our SDKs are open-source; the Electron SDK is part of the client-side JavaScript SDK, currently on the prerelease electron branch.

Why use this instead of the Node SDK?

Since Electron is based on Node.js, it is possible to run the LaunchDarkly Node SDK in it. However, this is strongly discouraged, as the Node SDK is meant for server-side use—not for applications that are distributed to users. There are several reasons why this distinction matters:

  • The server-side SDKs include an SDK key that can download the entire definition (including rollout rules and individual user targets) of all of your feature flags. If you embed this SDK key in an application, any user who looks inside the application can then access all of your feature flag definitions—which may include sensitive data such as other users' email addresses. The client-side and mobile SDKs use different credentials that do not allow this.
  • The server-side SDKs do in fact download your entire flag data using this key, since they have to be able to evaluate flags quickly for any user. That can be quite a large amount of data. The client-side and mobile SDKs, which normally evaluate flags for just one user at a time, use a much more efficient protocol where they request only the active variation for each flag for that specific user.
  • The Electron SDK also includes features that are specific to Electron, such as the ability to access main-process flags from the front end as described below.

Known issues in this release

  • If you are using the normal pattern of configuring your LaunchDarkly client in the main process, and then using initializeInRenderer() to get a mirror of the client in a renderer process, the client instance in the renderer process will not allow you to call identify() to change the current user. The current user can only be set in the main process. This may or may not change in future versions.
  • When the main process receives new feature flag values from LaunchDarkly, they should be sent to the renderer processes automatically; however, there was one report of this not working, i.e. the renderer process continued to have the old flag values. Please let us know if you see any such problems.

Getting started

You can install the SDK into your Electron project using npm:

npm install --save ldclient-electron

Initializing the client

Every Electron application consists of a main process, which is essentially a Node.js application, and some number of renderer processes, each of which is a Chromium web browser with its own window. These processes have their own independent JavaScript engines and data spaces, although there are ways to communicate between them.

The LaunchDarkly Electron SDK is designed to make it easy to use LaunchDarkly feature flags from within any of these processes. In the normal use case, there is an SDK client running in the main process; the renderer processes can then create client instances that are in effect mirrors of the main one.

To set up the main process client, you need the client-side ID for your LaunchDarkly environment, an object containing user properties (although you can change the user later), and optional configuration properties.

var LDElectron = require('ldclient-electron');

var user = { key: 'example' };
var options = {};
var client = LDElectron.initializeInMain('YOUR_CLIENT_SIDE_ID', user, options);

In a renderer process, to create a client object that uses the same feature flag data, you only need to do this:

var LDElectron = require('ldclient-electron');

var client = LDElectron.initializeInRenderer();

This gives you an object with the same interface—so you can evaluate feature flags, listen for flag change events, etc., in exactly the same way in the main process and the renderer process. However, only the main-process client is actually communicating with LaunchDarkly; the renderer-process clients are just delegating to the main one. This means that the overhead per application window is minimal (although it is still a good idea to retain a single client instance per window, rather than creating them ad-hoc when you need to evaluate a flag).

Both types of client are initialized asynchronously, so if you want to determine when the client is ready to evaluate feature flags, use the ready event or waitForInitialization():

// using an event listener:
client.on('ready', function() {
  // now we can evaluate some feature flags
});

// or, using a Promise:
client.waitForInitialization().then(function() {
  // now we can evaluate some feature flags
});

If you try to evaluate feature flags before the client is ready, it will behave as it would if no flags existed (i.e. variation will return a default value).

Bootstrapping

The bootstrap property in the client options allows you to speed up the startup process by providing an initial set of flag values.

If you set bootstrap to an object, the client will treat it as a map of flag keys to flag values. These values can be whatever you want (although in the browser environment, there is a special mechanism that is commonly used). The client will immediately start out in a ready state using these values. It will still make an initial request to LaunchDarkly to get the actual latest values, but that will happen in the background.

If you set bootstrap to the string "localstorage", the client will try to get flag values from persistent storage, using a unique key that is based on the user properties. In Electron, persistent storage consists of files in the userData directory. If the client finds flag values stored for this user, it uses them and starts up immediately in a ready state—but also makes a background request to LaunchDarkly to get the latest values, and stores them as soon as it receives them.

Feature flags

To evaluate any feature flag for the current user, call variation:

var showFeature = client.variation("YOUR_FEATURE_KEY", false);

if (showFeature)  {
  // feature flag is  on
} else {
  // feature flag is off
}

The return value of variation will always be either one of the variations you defined for your flag in the LaunchDarkly dashboard, or the default value. The default value is the second parameter to variation (in this case false) and it is what the client will use if it's not possible to evaluate the flag (for instance, if the flag key does not exist, or if something about the definition of the flag is invalid).

You can also fetch all flags for the current user:

var flags = client.allFlags();
var showFeature = flags['YOUR_FEATURE_KEY'];

This returns a key-value map of all your feature flags. It will contain null values for any flags that could not be evaluated.

Note that both of these methods are synchronous. The client always has the last known flag values in memory, so retrieving them does not involve any I/O.

Changing users

The identify() method tells the client to change the current user, and obtain the feature flag values for the new user. For example, on a sign-in page in a single-page app, you may initialize the client with an anonymous user; when the user logs in, you'd want the feature flag settings for the authenticated user.

If you provide a callback function, it will be called (with a map of flag keys and values) once the flag values for the new user are available; after that point, variation() will be using the new values. You can also use a Promise for the same purpose.

var newUser = { key: 'someone-else', name: 'John' };

client.identify(newUser, function(newFlags) {
  console.log('value of flag for this user is: ' + newFlags["YOUR_FEATURE_KEY"]);
  console.log('this should be the same: ' + client.variation("YOUR_FEATURE_KEY"));
});

// or:
client.identify(newUser).then(function(newFlags) {
  // as above
});

Note that the client always has one current user. The client-side SDKs are not designed for evaluating flags for different users at the same time.

Analytics events

Evaluating flags, either with variation() or with allFlags(), produces analytics events which you can observe on your LaunchDarkly Debugger page. Specifying a user with identify() (and also the initial user specified in the client constructor) also produces an analytics event, which is how LaunchDarkly receives your user data.

You can also explicitly send an event with any data you like using the track function:

client.track('my-custom-event-key', { customProperty: someValue });

You can completely disable event sending by setting sendEvents to false in the client options, but be aware that this means you will not have user data on your LaunchDarkly dashboard.

Receiving live updates

By default, the client requests feature flag values only once per user (i.e. once at startup time, and then each time you call identify()). You can also use a persistent connection to receive flag updates whenever they occur.

Setting streaming to true in the client options, or calling client.setStreaming(true), turns on this behavior. LaunchDarkly will push new values to the SDK, which will update the current feature flag state in the background, ensuring that variation() will always return the latest values.

If you want to be notified when a flag has changed, you can use an event listener for a specific flag:

client.on('change:YOUR_FEATURE_KEY', function(newValue, oldValue) {
  console.log('The flag was ' + oldValue + ' and now it is ' + newValue);
});

Or, you can listen for all feature flag changes:

client.on('change', function(allFlagChanges)) {
  Object.keys(allFlagChanges).forEach(function(key) {
    console.log('Flag ' + key + ' is now ' + allFlagChanges[key]);
  });
});

Subscribing to change events will automatically turn on streaming mode too, unless you have explicitly set streaming to false.