Skip to main content

Use Toggle to Roll out a Feature

We will guide you to use the FeatureProbe platform to control a back-end or front-end program, and let the back-end or front-end program display new features by percentage of received user requests.

Create a toggle on the platform

  1. Log in to the FeatureProbe demo platform. If you log in for the first time, please enter your email address. You can continue to use your email to access your data in the future.
  2. Click +Toggle to create a new toggle. add
  3. Set the name and key to tutorial_rollout, click Create and publish. create
  4. Click tutorial_rollout from the toggle list to open the targeting details page. list
  5. Change the return variation of the default rule to a percentage rollout. return
  6. Set 10% to open the toggle (return true), 90% to close the toggle (return false), and set the status to enabled. 10% true
  7. Click the Publish button below and Confirm the changes. confirm

Till now, the operation on the platform is complete. We have created a toggle to manage gradual rollout. Next, we will use it in the program to see the actual effect.

tip

After the toggle is created, it can be accessed in the back-end program or in the front-end program. Below we will introduce how to use it in Back-end code and Front-end code this toggle, the two are independent of each other, you can choose to read the part you are interested in according to your needs.

Control the backend program

We provide a backend code example, from which you can start to experience how the backend code uses the toggle.

Write code

  1. According to the language you are familiar with, download and open the corresponding back-end sample code.
git clone https://github.com/FeatureProbe/server-sdk-java.git
cd server-sdk-java

Open src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java file with an editor.

git clone https://github.com/FeatureProbe/server-sdk-java.git
cd server-sdk-java

Open src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java file with an editor.

  1. Open the FeatureProbe platform project list page, you can click Projects on the toggle details page to open project

  2. Copy Server SDK Key sdk key

  3. Fill in Server SDK Key and FeatureProbe remote URL ("https://featureprobe.io/server") into the corresponding variables of the backend code

src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java
    private static final String FEATURE_PROBE_SERVER_URL = "https://featureprobe.io/server";
private static final String FEATURE_PROBE_SERVER_SDK_KEY = // Fill in the server SDK key
  1. Add the following code to simulate 100 users accessing this toggle.
src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java
    public static void main(String[] args) throws IOException {

Logger root = (Logger)LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.WARN);

final FPConfig config = FPConfig.builder()
.remoteUri(FEATURE_PROBE_SERVER_URL)
.build();

// Init FeatureProbe, share this FeatureProbe instance in your project.
final FeatureProbe fpClient = new FeatureProbe(FEATURE_PROBE_SERVER_SDK_KEY, config);

//
final String YOUR_TOGGLE_KEY = "tutorial_rollout";
for (Integer i = 0; i < 100; i++) {
FPUser user = new FPUser();
Boolean isOpen = fpClient.boolValue(YOUR_TOGGLE_KEY, user, false);
System.out.println("feature for user " + i + " is :" + isOpen);
}
fpClient.close();
}
  1. Run the program.
mvn package
java -jar ./target/server-sdk-java-1.4.0.jar

Validate result

From the command line log, we can see that about 10% of the users entered the toggle, that is, they got the return variation value true.

log example
feature for user 0 is :false
feature for user 1 is :true
feature for user 2 is :false
feature for user 3 is :false
feature for user 4 is :false
feature for user 5 is :false
feature for user 6 is :false
feature for user 7 is :false
feature for user 8 is :false
feature for user 9 is :false
feature for user 10 is :false
feature for user 11 is :true
feature for user 12 is :false
feature for user 13 is :false
feature for user 14 is :false
feature for user 15 is :false
feature for user 16 is :false
feature for user 17 is :false
feature for user 18 is :false
feature for user 19 is :false
feature for user 20 is :false
tip

Every time the program is run, the user who enters the toggle may be different. If users with the same id always get the same toggle result, you need to use FPUser's stableRollout interface.

You can go back to the toggle targeting page of the platform, adjust the percentage, and then re-run the server program to see if the ratio of entering the toggle in the log has changed.

Control the front-end program

We provide a front-end js code example, and you can start to experience how the front-end code uses toggles.

Write code

  1. Clone the code.
git clone https://github.com/FeatureProbe/client-sdk-js.git
cd client-sdk-js
  1. Open platform to get client sdk key.
info

Click the "Projects" tab to enter the "Projects" list, obtain various SDK keys, and modify service and environment information.

client sdk key

  1. Open example/index.html and fill in Client SDK Key and FeatureProbe URL ("https://featureprobe.io/server")
example/index.html
const fpClient = new featureProbe.FeatureProbe({
remoteUrl: "https://featureprobe.io/server",
clientSdkKey: // Paste client sdk key here,
user,
refreshInterval: 5000,
});
  1. Simulate the current user accessing the toggle tutorial_rollout and get the toggle status directly
example/index.html
<script>
const user = new featureProbe.FPUser();
const fpClient = new featureProbe.FeatureProbe({
remoteUrl: "https://featureprobe.io/server",
clientSdkKey: // Paste client sdk key here,
user,
refreshInterval: 5000,
});

fpClient.start();
fpClient.on("ready", function() {
const boolValue = fpClient. boolValue("tutorial_rollout", false);
document.getElementById("boolean-result").innerText = boolValue;
});
</script>

Validate result

Open index.html in the browser, manually refresh the page (simulating multiple visits by the user), and you can see that the page can get the return value of true or false according to the configured percentage.

Demo
FeatureProbe JS SDK demo
This is a simple front-end-only application using FeatureProbe JS SDK.

boolean type
FeatureProbe evaluation boolean type toggle result is : true

You can go back to the toggle targeting page of the platform, adjust the percentage, and then refresh the page to see if the obtained true/false has changed.

tip

If you want the same user, no matter how he refreshes, he will always get a stable value. You need to Stable Rollout, and pass in the user's unique ID.