Home

Awesome

Important

Hobknob is deprecated and no longer actively supported.

If you are a contributor willing to take this project over then please get in touch via search-experience@opentable.com

hobknob-client-nodejs

A node client to retrieve feature toggles stored in Etcd.

Build Status

NPM

Installation

npm install hobknob-client-nodejs

Usage

var Client = require('hobknob-client-nodejs');

var client = new Client("application-name", {
    etcdHost: "127.0.0.1",
    etcdPort: 4001,
    cacheIntervalMs: 60000
});

client.on("error", function(err) {
    console.log(err);
});

client.on("updated-cache", function(togglesChanged){
    console.log('updated-cache' + JSON.stringify(togglesChanged)); // contains an array of toggles that changed in the last update
});

client.initialise(function(err) {

    if(err){
        throw err;
    }

    console.log(client.getOrDefault("toggle2", true));
});

Important Note

The "error" event must be subscribed to, otherwise errors will cause the application to exit

client.on("error", function(err) { });

Etcd

Feature toggles are stored in Etcd using the following convention: http://host:port/v2/keys/v1/toggles/applicationName/toggleName

API

Client(applicationName, [config])

Creates a new feature toggle client

var Client = require("hobknob-client-nodejs");
var client = new Client("application-name", { etcdHost: "127.0.0.1" });

.on(eventName, callback)

Subscribes to events emitted by the client.

Events:

example:

client.on('updated-cache', function(toggles){
  console.log(toggles);
});

// output
[
  { name: 'mytoggle', old: false, new: true },
  ...
]

.getOrDefault(toggleName, [secondaryKey], defaultValue)

Gets the value of a feature toggle (true or false) if exists, otherwise return the default value (true or false)

var isFeatureToggle1Enabled = client.getOrDefault('featureToggle1', false);

.getAll()

Gets the values for all features for the application.

var features = client.getAll();

features.should.be.eql({
  "feature1": "true",
  "feature2": "false",
  "domFeature/com": "true",
  "domFeature/couk": "false"
});