What is launchdarkly-js-client-sdk?
The launchdarkly-js-client-sdk is a JavaScript SDK for LaunchDarkly, a feature management platform that allows developers to control the release of features to users. This SDK enables you to manage feature flags, perform A/B testing, and roll out new features gradually.
What are launchdarkly-js-client-sdk's main functionalities?
Initialize the client
This code initializes the LaunchDarkly client with your environment key and a user object. The client is ready to be used once the 'ready' event is fired.
const LDClient = require('launchdarkly-js-client-sdk');
const client = LDClient.initialize('YOUR_ENVIRONMENT_KEY', { key: 'user_key' });
client.on('ready', () => {
console.log('LaunchDarkly client is ready');
});
Evaluate feature flags
This code evaluates a feature flag and returns its value. If the feature flag is enabled, it will log 'Feature is enabled'; otherwise, it will log 'Feature is disabled'.
client.on('ready', () => {
const showFeature = client.variation('your-feature-flag-key', false);
if (showFeature) {
console.log('Feature is enabled');
} else {
console.log('Feature is disabled');
}
});
Track custom events
This code tracks a custom event with a specified key and optional data. This can be useful for analytics and monitoring user interactions.
client.track('custom-event-key', { customData: 'value' });
Identify users
This code identifies a new user with a unique key and optional attributes. This is useful for targeting specific users with feature flags.
client.identify({ key: 'new_user_key', name: 'New User' }).then(() => {
console.log('User identified');
});
Other packages similar to launchdarkly-js-client-sdk
unleash-client
Unleash is an open-source feature management solution. The unleash-client package provides similar functionality to LaunchDarkly, allowing you to manage feature flags and control feature rollouts. However, Unleash is self-hosted, which means you need to manage the infrastructure yourself.
configcat-js
ConfigCat is a feature flag and configuration management service. The configcat-js package allows you to manage feature flags and configurations in your JavaScript applications. ConfigCat offers a hosted solution with a user-friendly interface, similar to LaunchDarkly.
flagsmith
Flagsmith is an open-source feature flag and remote config service. The flagsmith package provides similar functionality to LaunchDarkly, including feature flag management and user targeting. Flagsmith can be used as a hosted service or self-hosted, offering flexibility in deployment.
LaunchDarkly SDK for Browser JavaScript
LaunchDarkly overview
LaunchDarkly is a feature management platform that serves over 100 billion feature flags daily to help teams build better software, faster. Get started using LaunchDarkly today!
Getting started
Refer to the SDK documentation for instructions on getting started with using the SDK.
Note: If you are using JavaScript in a non-browser environment, please see our server-side Node.js SDK, client-side Node.js SDK, and Electron SDK.
Please note that the JavaScript SDK has two special requirements in terms of your LaunchDarkly environment. First, in terms of the credentials for your environment that appear on your Account Settings dashboard, the JavaScript SDK uses the "Client-side ID"-- not the "SDK key" or the "Mobile key". Second, for any feature flag that you will be using in JavaScript code, you must check the "Make this flag available to client-side SDKs" box on that flag's Settings page.
ReactJS
The SDK does not require any particular JavaScript framework. However, if you are using React, there is an add-on to simplify use of the SDK. See react-client-sdk
.
Browser compatibility
The LaunchDarkly SDK can be used in all major browsers. However, web browsers vary widely in their support of specific features and standards. Three features that are used by the LaunchDarkly SDK that may not be available on every browser are Promise
, EventSource
, and document.querySelectorAll()
. For more information on whether you may need to use a polyfill to ensure compatibility, and how to do so, see "JS SDK requirements and polyfills".
Logging
By default, the SDK sends log output to the browser console. There are four logging levels: debug
, info
, warn
, and error
; by default, debug
and info
messages are hidden. See LDOptions.logger
and createConsoleLogger
for more details.
Learn more
Check out our documentation for in-depth instructions on configuring and using LaunchDarkly. You can also head straight to the complete reference guide for this SDK. Additionally, the authoritative full description of all properties, types, and methods is the online TypeScript documentation. If you are not using TypeScript, then the types are only for your information and are not enforced, although the properties and methods are still the same as described in the documentation.
For examples of using the SDK in a simple JavaScript application, see hello-js
and hello-bootstrap
.
Testing
We run integration tests for all our SDKs using a centralized test harness. This approach gives us the ability to test for consistency across SDKs, as well as test networking behavior in a long-running application. These tests cover each method in the SDK, and verify that event sending, flag evaluation, stream reconnection, and other aspects of the SDK all behave correctly.
Contributing
We encourage pull requests and other contributions from the community. Check out our contributing guidelines for instructions on how to contribute to this SDK.
About LaunchDarkly
- LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can:
- Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases.
- Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?).
- Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file.
- Grant access to certain features based on user attributes, like payment plan (eg: users on the ‘gold’ plan get access to more features than users in the ‘silver’ plan). Disable parts of your application to facilitate maintenance, without taking everything offline.
- LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Check out our documentation for a complete list.
- Explore LaunchDarkly