What is unleash-proxy-client?
The unleash-proxy-client npm package is a client library for the Unleash Proxy, which is a feature management system. It allows you to control feature toggles in your application, enabling or disabling features without deploying new code. This is particularly useful for A/B testing, gradual rollouts, and canary releases.
What are unleash-proxy-client's main functionalities?
Initialize Client
This code initializes the Unleash client with the necessary configuration such as the URL of the Unleash Proxy, the client key, and the application name. The `start` method is called to begin fetching feature toggles.
const { UnleashClient } = require('unleash-proxy-client');
const unleash = new UnleashClient({
url: 'https://app.unleash-hosted.com/demo/api/frontend',
clientKey: 'your-client-key',
appName: 'my-app'
});
unleash.start();
Check Feature Toggle
This code checks if a specific feature toggle is enabled. The `isEnabled` method returns a boolean indicating the status of the feature toggle.
const isEnabled = unleash.isEnabled('feature-toggle-name');
console.log(`Feature is enabled: ${isEnabled}`);
Register Event Listeners
This code registers event listeners for the Unleash client. The `update` event is triggered when feature toggles are updated, and the `error` event is triggered if there is an error fetching feature toggles.
unleash.on('update', () => {
console.log('Feature toggles updated');
});
unleash.on('error', (error) => {
console.error('Error fetching feature toggles:', error);
});
Other packages similar to unleash-proxy-client
launchdarkly-node-client-sdk
LaunchDarkly is a feature management platform that allows you to control the release of features to users. It offers similar functionality to Unleash, such as feature toggles, A/B testing, and gradual rollouts. The main difference is that LaunchDarkly is a commercial product with more advanced analytics and targeting capabilities.
configcat-node
ConfigCat is a feature flag and configuration management service. It provides similar functionalities to Unleash, including feature toggles and remote configuration. ConfigCat is known for its simplicity and ease of use, making it a good alternative for smaller projects or teams.
flagr
Flagr is an open-source feature flagging and A/B testing service. It allows you to create and manage feature flags and run experiments. Flagr is similar to Unleash in that it is open-source and can be self-hosted, but it also includes built-in support for A/B testing and experimentation.
Unleash Proxy Client for the browser (js)
This is a tiny Unleash Client SDK you can use together with the
Unleash Hosted Proxy.
This makes it super simple to use Unleash-hosted from any single page app.
This client expect fetch
to be available. If you have to support older
browsers you should probably use the fetch polyfill.
How to use the client as a module.
Step 1: Install
npm install unleash-proxy-client --save
Step 2: Initialize
You need to have a Unleash-hosted instance, and the proxy need to be enabled. In addition you will need a proxy-specific clientKey
in order to connect to the Unleash-hosted Proxy.
import { UnleashClient } from 'unleash-proxy-client';
const unleash = new UnleashClient({
url: 'https://eu.unleash-hosted.com/hosted/api',
clientKey: 'your-proxy-key'
});
unleash.updateContext({userId: '1233'});
unleash.start();
Step 3: Check if feature toggle is enabled
unleash.isEnabled('proxy.demo');
Step 4: Get toggle variant
const variant = unleash.getVariant('proxy.demo');
if(variant.name === 'blue') {
}
Listen for updates via the EventEmitter
The client is also an event emitter. This means that your code can subscribe to updates from the client. This is a neat way to update a single page app when toggle state updates.
unleash.subscribe('update', () => {
const myToggle = unleash.isEnabled('proxy.demo');
});
How to use the client via CDN.
<html>
<head>
<script src="https://unpkg.com/unleash-proxy-client@latest/build/main.min.js" type="text/javascript"></script>
<script type="text/javascript">
var config = {url: 'https://eu.unleash-hosted.com/hosted/api', clientKey: 'some-proxy-key'};
var context = {userId: '1233'};
var client = new unleash.UnleashClient(config, context);
client.start();
setTimeout(() => {
console.log(client.isEnabled('demo.toggle'));
}, 1000);
</script>
</head>
</html>