Blog > Tutorial

Using Feature Flags in Node

Posted by AppFlags | September 8, 2023

Using Feature Flags in Node

Feature flags are a technique used in software development to enable or disable certain features of an application. They allow developers to control the release of new features or modifications to existing features, providing a way to gradually roll out new functionality or experiment with different versions of a feature. Feature flags also enable developers to quickly turn off features if something goes wrong or if a feature needs to be temporarily disabled. This can help reduce the risk of deploying new code, increase agility, and improve the overall quality of the software.

In this tutorial, we discuss a few beneficial attributes of the AppFlags Node SDK and then show how to use AppFlags to implement feature flags in your Node application.

Attributes of the AppFlags Node SDK

There are a few attributes of the AppFlags Node SDK that are not immediately obvious, but are very beneficial to your application: local evaluation and realtime updates.

Local evaluation

Performance is important for any Node application. That is why the AppFlags Node SDK evaluates feature flags locally on your server, rather than in the cloud. The Node SDK downloads your feature flag configuration to your server and computes feature flags locally in a fraction of a millisecond*. As a result, using AppFlags has basically no performance impact on your application.

* Benchmarked at 0.03 ms on a modest server.

Realtime updates

Any changes made to your feature flags immediately update in your Node application with Realtime Updates. This has several advantages:

  • You don't need to wait and refresh for changes to take effect.
  • Disabling a feature in an emergency happens instantly.
  • All of your apps (using any SDK) immediately have the same updated feature flags.

Getting Started with the AppFlags Node SDK

Add the AppFlags SDK to your project

Install the appflags-node dependency in your project.

For example, if you are using npm:

npm install appflags-node

Initialize AppFlags in your app

Create an AppFlags client with your server-side SDK key:

const {AppFlagsClient} = require("appflags-node");
    
const appflagsClient = new AppFlagsClient("YOUR_SDK_KEY");

Wait for the AppFlags client to initialize while it downloads your feature flag configuration to prepare for local feature flag evaluation:

Using async/await:

await appflagsClient.onInitialized();

Using promises:

appflagsClient.onInitialized().then(() => {
    // the client is initialized and ready to use
});

Providing user data

In order to call the AppFlags SDK to evaluate feature flags for a user, you must provide a user object. A minimal user object contains a unique key identfiying the user.

const user = {
    key: "UNIQUE_USER_KEY"
}

Retrieve a feature flag

Use the getFlag function to retrieve a single feature flag for a user.

const flag = appflagsClient.getFlag(user, "flag_key");

Retrieve all feature flags

Use the getAllFlags function to retrieve all feature flags for a particular user.

const flags = appflagsClient.getAllFlags(user);

Listen for realtime updates

Use the onFlagsChanged callback to get notified in realtime when your feature flag configuraton has changed.

appflagsClient.onFlagsChanged(() => {
    // the feature flag configuration has changed
})

Example project

An example project for using AppFlags in Node can be found here: https://github.com/AppFlags/example-node

Conclusion

As you can see in these examples, feature flags are a simple yet powerful tool that you can use to control the release of new features or modifications to existing features in your Node application.

Ready to try feature flags in your application?

Try AppFlags for free. No credit card required.

Subscribe to our blog

Get the latests posts in your email

Thanks for signing up for the AppFlags blog!

Error sending please try again

Popular Posts