Get notified when feature rollout rules change #1108
-
Hello, My setup is as follows:
In the code, using It is possible to get the live-update in this case? As a workaround, it works after I refresh the browser, but my expectation is to receive the live-update. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
I assume you are using polling client? How often you poll for the features to update? It is a Cheers |
Beta Was this translation helpful? Give feedback.
-
Hi Irina, thanks for your answer! I did not set any interval, just I'm using the SEE connection method as it is documented here: https://www.npmjs.com/package/featurehub-javascript-client-sdk?activeTab=readme#changing-to-sse-server-sent-events---real-time-streaming-updates And also set the listener for the specific feature to be notified in real-time to get new values like this: https://www.npmjs.com/package/featurehub-javascript-client-sdk?activeTab=readme#feature-updates-listener This is my current code that initialize the Featurehub client and listen for updates. async loadFeaturehub() {
if (!this._loadFeatureHubState) {
await this.createFeatureHubClient(this.credentials)
return new Promise((resolve, reject) => {
this._featureHubConfig.addReadinessListener((newReadyState, firstTimeReady) => {
if (firstTimeReady) {
const flags = this._readFlagsFromFeatureHub()
this._listenForFeatureUpdates(flags)
this._loadFeatureHubState = true
resolve(flags)
}
})
})
}
}
async createFeatureHubClient(credentials) {
// swapping to the Streaming connector (SSE)
EdgeFeatureHubConfig.defaultEdgeServiceSupplier = (repository, config) => {
return new FeatureHubEventSourceClient(config, repository)
}
this.featureHubConfig = new EdgeFeatureHubConfig(credentials.host, credentials.key)
this.featureHubClient = await this._featureHubConfig.newContext().build()
}
readFlagsFromFeatureHub() {
const flags = this.featureHubClient.repository().features
return this.getFeaturesMap(flags)
}
listenForFeatureUpdates(flagsList) {
Object.keys(flagsList).forEach(flag => {
this.featureHubClient.feature(flag).addListener(feature => {
// Save the new feature value
this.updateFeature(flag)
})
}
}
async updateFeaturesUserContext(key, value) {
this.featureHubClient.attributeValue(key, value)
await this.featureHubClient.build()
} First of all I call My problem is that the listener didn't handle updates in case a rule if changed in fh dashboard |
Beta Was this translation helpful? Give feedback.
-
Hey there! Its expected that the listener, even when filtering, will trigger updates, but the triggers occur off your current context, and you get a tree of contexts, so if you did this for example: let listener1 = client.feature('x').addListener(....); then they will trigger off different contexts, when this builds the first context, it remembers the values and triggers only off those values. That would be my first idea of what is possibly being unexpected? What you absolutely should see is a listener being fired when it is attached to a context with the appropriate attributes already set. If you could perhaps include in the example of when the updateFeatureUserContext is called? Otherwise I would recommend storing the listeners and unlistening and reattaching the listeners when you change the context but before you do the build(). |
Beta Was this translation helpful? Give feedback.
-
Hello again, async loadFeaturehub() {
if (!this._loadFeatureHubState) {
await this.createFeatureHubClient(this.credentials)
return new Promise((resolve, reject) => {
this._featureHubConfig.addReadinessListener((newReadyState, firstTimeReady) => {
if (firstTimeReady) {
const flags = this.readFlagsFromFeatureHub()
this.listenForFeatureUpdates(flags)
this._loadFeatureHubState = true
resolve(flags)
}
})
})
}
}
async createFeatureHubClient(credentials) {
// swapping to the Streaming connector (SSE)
EdgeFeatureHubConfig.defaultEdgeServiceSupplier = (repository, config) => {
return new FeatureHubEventSourceClient(config, repository)
}
this.featureHubConfig = new EdgeFeatureHubConfig(credentials.host, credentials.key)
this.featureHubClient = await this._featureHubConfig.newContext().build()
}
readFlagsFromFeatureHub() {
const flags = this.featureHubClient.repository().features
return this.getFeaturesMap(flags)
}
listenForFeatureUpdates(flagsList) {
Object.keys(flagsList).forEach(flag => {
this.featureHubClient.feature(flag).addListener(feature => {
// Save the new feature value
this.updateFeature(flag)
})
}
}
async updateFeaturesUserContext(key, value) {
this.featureHubClient.attributeValue(key, value)
await this.featureHubClient.build()
this.renewListeners()
}
renewListeners() {
this.clearListeners()
const flags = this.readFlagsFromFeatureHub()
Object.keys(flags).forEach(flag => {
this.updateFeature(flag)
})
this.listenForFeatureUpdates(flags)
}
getFeaturesMap(flags) {
const resultFlags = {}
for (const [key, val] of flags) {
switch (val.type) {
case 'Boolean': {
// FeatureHub didn't support '-' in its flag keys so we need to replace '-' by '_'
resultFlags[normalizeFlagKey(key)] = val.value
break
}
case 'json': {
let parsedVal = {}
try {
parsedVal = JSON.parse(val.rawJson)
} catch (e) {
console.error(`%c There was an error parsing value for key: ${key}.\n ${e}.\n Check in Featurehub the ${key} has a defined value"`, 'color: #E9D502')
}
// FeatureHub didn't support '-' in its flag keys so we need to replace by '_'
resultFlags[normalizeFlagKey(key)] = parsedVal
break
}
}
}
return resultFlags
} When my app initializes, first of all is to initializes featuremanager by calling |
Beta Was this translation helpful? Give feedback.
-
Hey there, so I have replicated this in a test in a branch, so give me a few days to get back to you with an update and I will have it working, that ok? As a side note, why do you say that FH keys cannot contain "-"? They can't for react because react doesn't like them, but they can contain pretty much anything... |
Beta Was this translation helpful? Give feedback.
-
added an issue here to track this #1108 |
Beta Was this translation helpful? Give feedback.
-
This is fixed now with an update to the js-sdk |
Beta Was this translation helpful? Give feedback.
-
🤩 Kudos and cheers to the team! |
Beta Was this translation helpful? Give feedback.
This is fixed now with an update to the js-sdk