All Projects β†’ Unleash β†’ Unleash Client Node

Unleash / Unleash Client Node

Licence: apache-2.0
Unleash client SDK for Node.js

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Unleash Client Node

Featuretoggle
Simple, reliable feature toggles in .NET
Stars: ✭ 641 (+367.88%)
Mutual labels:  feature-flags, feature-toggles
Flopflip
🎚Flip or flop features in your React application in real-time backed by flag provider of your choice 🚦
Stars: ✭ 334 (+143.8%)
Mutual labels:  feature-flags, feature-toggles
Flags
⛳️ Feature Flags for Next.js
Stars: ✭ 277 (+102.19%)
Mutual labels:  feature-flags, feature-toggles
Molasses
Feature toggle library for elixir
Stars: ✭ 70 (-48.91%)
Mutual labels:  feature-flags, feature-toggles
Flagsmith Frontend
Web App and Mobile App for Flagsmith
Stars: ✭ 86 (-37.23%)
Mutual labels:  feature-flags, feature-toggles
Flipt
An open-source, on-prem feature flag solution
Stars: ✭ 1,623 (+1084.67%)
Mutual labels:  feature-flags, feature-toggles
Fun with flags
Feature Flags/Toggles for Elixir
Stars: ✭ 554 (+304.38%)
Mutual labels:  feature-flags, feature-toggles
react-client-sdk
LaunchDarkly Client-side SDK for React.js
Stars: ✭ 42 (-69.34%)
Mutual labels:  feature-flags, feature-toggles
Unleash Client Python
Unleash client for Python πŸ’‘πŸ’‘πŸ’‘
Stars: ✭ 44 (-67.88%)
Mutual labels:  feature-flags, feature-toggles
Piranha
A tool for refactoring code related to feature flag APIs
Stars: ✭ 1,840 (+1243.07%)
Mutual labels:  feature-flags, feature-toggles
Tweek
Tweek - an open source feature manager
Stars: ✭ 268 (+95.62%)
Mutual labels:  feature-flags, feature-toggles
Unleash Client Go
Unleash Client for Go
Stars: ✭ 78 (-43.07%)
Mutual labels:  feature-flags, feature-toggles
python-client
Python SDK client for Split Software
Stars: ✭ 12 (-91.24%)
Mutual labels:  feature-flags, feature-toggles
Flipper
Flipper is a simple and useful tool to deal with feature toggles
Stars: ✭ 64 (-53.28%)
Mutual labels:  feature-flags, feature-toggles
ios-client-sdk
LaunchDarkly Client-side SDK for iOS (Swift and Obj-C)
Stars: ✭ 45 (-67.15%)
Mutual labels:  feature-flags, feature-toggles
Unleash
Unleash is the open source feature toggle service.
Stars: ✭ 4,679 (+3315.33%)
Mutual labels:  feature-flags, feature-toggles
ruby-server-sdk
LaunchDarkly Server-side SDK for Ruby
Stars: ✭ 25 (-81.75%)
Mutual labels:  feature-flags, feature-toggles
toggler
toggler is a feature flag service to decouple deployment, feature enrollment and experiments
Stars: ✭ 27 (-80.29%)
Mutual labels:  feature-flags, feature-toggles
Feature Flags
Feature flags API written in Go
Stars: ✭ 375 (+173.72%)
Mutual labels:  feature-flags, feature-toggles
Flagr
Flagr is a feature flagging, A/B testing and dynamic configuration microservice
Stars: ✭ 1,776 (+1196.35%)
Mutual labels:  feature-flags, feature-toggles

Unleash Client SDK for Node.js

npm npm Build Status Code Climate Coverage Status

Unleash Client SDK for Node.js. It is compatible with:

Getting started

1. Install the unleash-client into your project

$ npm install unleash-client --save

2. Initialize unleash-client

It is recommended to initialize the Unleash client SDK as early as possible in your node.js application. The SDK will set-up a in-memory repository, and poll updates from the unleash-server at regular intervals.

const { initialize } = require('unleash-client');
const unleash = initialize({
  url: 'http://unleash.herokuapp.com/api/',
  appName: 'my-app-name',
  instanceId: 'my-unique-instance-id',
});

unleash.on('synchronized', () => {
  // Unleash is ready to serve updated feature toggles.

  // Check a feature flag
  const isEnabled = unleash.isEnabled('some-toggle');

  // Check the variant
  const variant = unleash.getVariant('app.ToggleY');
});

Be aware that the initialize function will configure a global Unleash instance. If you call this method multiple times the global instance will be changed. If you prefer to handle the instance yourself you should construct your own Unleash instance.

Block until Unleash SDK has synchronized

You can also use the startUnleash function, and await for the SDK to have fully synchronized with the unleash-api. This allows you to secure that the SDK is not operating on locally and potential stale feature toggle configuration.

const { startUnleash } = require('unleash-client');

const unleash = await startUnleash({
  appName: 'async-unleash',
  url: 'http://unleash.herokuapp.com/api/',
});

// Unleash SDK has now fresh state from the unleash-api
const isEnabled = unleash.isEnabled('Demo');

4. Stop unleash

To shut down the client (turn off the polling) you can simply call the destroy-method. This is typically not required.

const { destroy } = require('unleash-client');
destroy();

Built in activation strategies

The client comes with implementations for the built-in activation strategies provided by unleash.

  • DefaultStrategy
  • UserIdStrategy
  • FlexibleRolloutStrategy
  • GradualRolloutUserIdStrategy
  • GradualRolloutSessionIdStrategy
  • GradualRolloutRandomStrategy
  • RemoteAddressStrategy
  • ApplicationHostnameStrategy

Read more about the strategies in activation-strategy.md.

Unleash context

In order to use some of the common activation strategies you must provide a unleash-context. This client SDK allows you to send in the unleash context as part of the isEnabled call:

const unleashContext = {
  userId: '123',
  sessionId: 'some-session-id',
  remoteAddress: '127.0.0.1',
};
unleash.isEnabled('someToggle', unleashContext);

Advanced usage

The initialize method takes the following arguments:

  • url - the url to fetch toggles from. (required)
  • appName - the application name / codebase name (required)
  • instanceId - an unique identifier, should/could be somewhat unique
  • refreshInterval - The poll-intervall to check for updates. Defaults to 15000ms.
  • metricsInterval - How often the client should send metrics to Unleash API. Defaults to 60000ms.
  • strategies - Custom activation strategies to be used.
  • disableMetrics - disable metrics
  • customHeaders - Provide a map(object) of custom headers to be sent to the unleash-server
  • customHeadersFunction - Provide a function that return a Promise resolving as custom headers to be sent to unleash-server. When options are set, this will take precedence over customHeaders option.
  • timeout - specify a timeout in milliseconds for outgoing HTTP requests. Defaults to 10000ms.
  • repository - Provide a custom repository implementation to manage the underlying data

Custom strategies

1. implement the custom strategy:

const { Strategy, initialize } = require('unleash-client');
class ActiveForUserWithEmailStrategy extends Strategy {
  constructor() {
    super('ActiveForUserWithEmail');
  }

  isEnabled(parameters, context) {
    return parameters.emails.indexOf(context.email) !== -1;
  }
}

2. register your custom strategy:

initialize({
  url: 'http://unleash.herokuapp.com',
  strategies: [new ActiveForUserWithEmailStrategy()],
});

Alternative usage

Its also possible to ship the unleash instance around yourself, instead of using on the default require.cache to have share one instance.

const { Unleash } = require('unleash-client');

const unleash = new Unleash({
  appName: 'my-app-name',
  url: 'http://unleash.herokuapp.com',
});

unleash.on('ready', console.log.bind(console, 'ready'));
// required error handling when using unleash directly
unleash.on('error', console.error);

Events

The unleash instance object implements the EventEmitter class and emits the following events:

event payload description
ready - is emitted once the fs-cache is ready. if no cache file exists it will still be emitted. The client is ready to use, but might not have synchronized with the Unleash API yet. This means the SDK still can operate on stale configurations.
synchronized - is emitted when the SDK has successfully synchronized with the Unleash API and has all the latest feature toggle configuration available.
registered - is emitted after the app has been registered at the api server
sent object data key/value pair of delivered metrics
count string name, boolean enabled is emitted when a feature is evaluated
warn string msg is emitted on a warning
error Error err is emitted on a error
unchanged - is emitted each time the client gets new toggle state from server, but nothing has changed
changed object data is emitted each time the client gets new toggle state from server and changes has been made

Example usage:

const { initialize, isEnabled } = require('unleash-client');

const instance = initialize({
    appName: 'my-app-name',
    url: 'http://unleash.herokuapp.com/api/',
});

// Some useful life-cycle events
unleash.on('ready', console.log);
unleash.on('synchronized', console.log);
unleash.on('error', console.error);
unleash.on('warn', console.warn);

instance.once('registered', () => {
    // Do something after the client has registered with the server api.
    // NB! It might not have received updated feature toggles yet.
});

instance.once('changed', () => {
    console.log(`Demo is enabled: ${isEnabled('Demo')}`);
});

unleash.on('count', (name, enabled) => console.log(`isEnabled(${name})

Toggle definitions

Sometimes you might be interested in the raw feature toggle definitions.

const {
  initialize,
  getFeatureToggleDefinition,
  getFeatureToggleDefinitions,
} = require('unleash-client');

initialize({
  url: 'http://unleash.herokuapp.com/api/',
  appName: 'my-app-name',
  instanceId: 'my-unique-instance-id',
});

const featureToogleX = getFeatureToggleDefinition('app.ToggleX');
const featureToggles = getFeatureToggleDefinitions();

Custom repository

You can manage the underlying data layer yourself if you want to. This enables you to use unleash offline, from a browser environment or implement your own caching layer. See example.

Unleash depends on a ready event of the repository you pass in. Be sure that you emit the event after you've initialized unleash.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].