Skip to content

Commit 129dbcb

Browse files
add key-value collection refresh
1 parent 10254aa commit 129dbcb

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

articles/azure-app-configuration/enable-dynamic-configuration-javascript.md

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: zhiyuanliang-ms
77
ms.service: azure-app-configuration
88
ms.devlang: javascript
99
ms.topic: tutorial
10-
ms.date: 03/27/2024
10+
ms.date: 01/20/2025
1111
ms.custom: devx-track-js
1212
ms.author: zhiyuanliang
1313
#Customer intent: As a JavaScript developer, I want to dynamically update my app to use the latest configuration data in Azure App Configuration.
@@ -31,8 +31,10 @@ Add the following key-value to your Azure App Configuration store. For more info
3131
| *message* | *Hello World!* | Leave empty | Leave empty |
3232
| *sentinel* | *1* | Leave empty | Leave empty |
3333

34+
A *sentinel key* is a key that you update after you complete the change of all other keys. Your app monitors the sentinel key. When a change is detected, your app refreshes all configuration values. This approach helps to ensure the consistency of configuration in your app and reduces the overall number of requests made to your Azure App Configuration store, compared to monitoring all keys for changes.
35+
3436
> [!NOTE]
35-
> A *sentinel key* is a key that you update after you complete the change of all other keys. Your app monitors the sentinel key. When a change is detected, your app refreshes all configuration values. This approach helps to ensure the consistency of configuration in your app and reduces the overall number of requests made to your Azure App Configuration store, compared to monitoring all keys for changes.
37+
> If you use version **2.0.0-preview.2** or later of [@azure/app-configuration-provider](https://www.npmjs.com/package/@azure/app-configuration-provider), the App Configuration provider will by default refresh based on monitoring the key-value collection. Sentinel key is not needed for refresh. For more information, please go to [Monitor key-value collection for refresh](#monitor-key-value-collection-for-refresh)
3638
3739
## Console applications
3840

@@ -49,7 +51,7 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
4951

5052
```javascript
5153
// Connecting to Azure App Configuration using endpoint and token credential
52-
const settings = await load(endpoint, credential, {
54+
const appConfig = await load(endpoint, credential, {
5355
// Setting up to refresh when the sentinel key is changed
5456
refreshOptions: {
5557
enabled: true,
@@ -65,7 +67,7 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
6567

6668
```javascript
6769
// Connecting to Azure App Configuration using endpoint and token credential
68-
const settings = await load(endpoint, credential, {
70+
const appConfig = await load(endpoint, credential, {
6971
// Setting up to refresh when the sentinel key is changed
7072
refreshOptions: {
7173
enabled: true,
@@ -74,11 +76,11 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
7476
});
7577

7678
// Constructing configuration object
77-
let config = settings.constructConfigurationObject();
79+
let config = appConfig.constructConfigurationObject();
7880

7981
// Setting up callback to ensure `config` object is updated once configuration is changed.
80-
settings.onRefresh(() => {
81-
config = settings.constructConfigurationObject();
82+
appConfig.onRefresh(() => {
83+
config = appConfig.constructConfigurationObject();
8284
});
8385

8486
```
@@ -94,8 +96,8 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
9496
// Polling for configuration changes every 5 seconds
9597
while (true) {
9698
await sleepInMs(5000); // Waiting before the next refresh
97-
await settings.refresh(); // Refreshing the configuration setting
98-
console.log(settings.get("message")); // Consume current value of message from a Map
99+
await appConfig.refresh(); // Refreshing the configuration setting
100+
console.log(appConfig.get("message")); // Consume current value of message from a Map
99101
}
100102
```
101103

@@ -105,7 +107,7 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
105107
// Polling for configuration changes every 5 seconds
106108
while (true) {
107109
await sleepInMs(5000); // Waiting before the next refresh
108-
await settings.refresh(); // Refreshing the configuration setting
110+
await appConfig.refresh(); // Refreshing the configuration setting
109111
console.log(config.message); // Consume current value of message from an object
110112
}
111113
```
@@ -125,7 +127,7 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
125127
126128
async function run() {
127129
// Connecting to Azure App Configuration using endpoint and token credential
128-
const settings = await load(endpoint, credential, {
130+
const appConfig = await load(endpoint, credential, {
129131
// Setting up to refresh when the sentinel key is changed
130132
refreshOptions: {
131133
enabled: true,
@@ -136,8 +138,8 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
136138
// Polling for configuration changes every 5 seconds
137139
while (true) {
138140
await sleepInMs(5000); // Waiting before the next refresh
139-
await settings.refresh(); // Refreshing the configuration setting
140-
console.log(settings.get("message")); // Consume current value of message from a Map
141+
await appConfig.refresh(); // Refreshing the configuration setting
142+
console.log(appConfig.get("message")); // Consume current value of message from a Map
141143
}
142144
}
143145
@@ -154,7 +156,7 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
154156
155157
async function run() {
156158
// Connecting to Azure App Configuration using endpoint and token credential
157-
const settings = await load(endpoint, credential, {
159+
const appConfig = await load(endpoint, credential, {
158160
// Setting up to refresh when the sentinel key is changed
159161
refreshOptions: {
160162
enabled: true,
@@ -163,17 +165,17 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
163165
});
164166
165167
// Constructing configuration object
166-
let config = settings.constructConfigurationObject();
168+
let config = appConfig.constructConfigurationObject();
167169
168170
// Setting up callback to ensure `config` object is updated once configuration is changed.
169-
settings.onRefresh(() => {
170-
config = settings.constructConfigurationObject();
171+
appConfig.onRefresh(() => {
172+
config = appConfig.constructConfigurationObject();
171173
});
172174
173175
// Polling for configuration changes every 5 seconds
174176
while (true) {
175177
await sleepInMs(5000); // Waiting before the next refresh
176-
await settings.refresh(); // Refreshing the configuration setting
178+
await appConfig.refresh(); // Refreshing the configuration setting
177179
console.log(config.message); // Consume current value of message from an object
178180
}
179181
}
@@ -211,6 +213,23 @@ You can connect to App Configuration using either Microsoft Entra ID (recommende
211213
Hello World - Updated!
212214
```
213215

216+
## Monitor key-value collection for refresh
217+
218+
Instead of monitoring any sentinel key, the App Configuration provider supports for monitoring all selected key-values. Configuration will be refreshed if any of key-values are updated. Watching the sentinel key for refresh helps ensure data integrity of configuration changes, but it is now optional. This behavior is activated when you enable the refresh but do not specify any watched keys in `AzureAppConfigurationOptions.refreshOptions`
219+
220+
```javascript
221+
const appConfig = await load(endpoint, credential, {
222+
refreshOptions: {
223+
enabled: true,
224+
// watchedSettings: []
225+
}
226+
});
227+
228+
appConfig.refresh(); // Configuration will be refreshed if any of key-values are updated.
229+
```
230+
231+
This feature is available for version **2.0.0-preview.2** or later of [@azure/app-configuration-provider](https://www.npmjs.com/package/@azure/app-configuration-provider).
232+
214233
## Server application
215234

216235
The following example shows how to update an existing http server to use refreshable configuration values.
@@ -265,7 +284,7 @@ The following example shows how to update an existing http server to use refresh
265284
appConfig = await load(endpoint, credential, {
266285
refreshOptions: {
267286
enabled: true,
268-
watchedSettings: [{ key: "sentinel" }], // Watch for changes to the key "sentinel" and refreshes the configuration when it changes
287+
// without registering any watched setting/sentinel key, the provider will monitor the key-value collection for refresh
269288
refreshIntervalInMs: 15_000 // Set the refresh interval
270289
}
271290
});
@@ -315,12 +334,11 @@ We recommend to implement request-driven configuration refresh for your web appl
315334
> [!div class="mx-imgBorder"]
316335
> ![Screenshot of browser with a message from App Configuration.](./media/dynamic-refresh-javascript/http-server.png)
317336
318-
1. Update the following key-values to the Azure App Configuration store. Update value of the key `message` first and then `sentinel`.
337+
1. Update the following key-values to the Azure App Configuration store. Update value of the key `message`.
319338
320339
| Key | Value | Label | Content type |
321340
|----------------|---------------------------|-------------|--------------------|
322341
| *message* | *Hello World - Updated!* | Leave empty | Leave empty |
323-
| *sentinel* | *3* | Leave empty | Leave empty |
324342
325343
1. After about 15 seconds, refresh the page and the message should be updated.
326344

0 commit comments

Comments
 (0)