Skip to content

Commit 28367ef

Browse files
authored
Update examples and docs for subscribing to configuration stores (#524)
* Update examples for config API Signed-off-by: Shubham Sharma <[email protected]> * Lint Signed-off-by: Shubham Sharma <[email protected]> * Update package-lock.json Signed-off-by: Shubham Sharma <[email protected]> * Add examples for configuration subscribe Signed-off-by: Shubham Sharma <[email protected]> * Remove ==APP== from logs Signed-off-by: Shubham Sharma <[email protected]> --------- Signed-off-by: Shubham Sharma <[email protected]>
1 parent bec157c commit 28367ef

File tree

5 files changed

+128
-34
lines changed

5 files changed

+128
-34
lines changed

daprdocs/content/en/js-sdk-docs/js-client/_index.md

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,10 +436,13 @@ start().catch((e) => {
436436
import { DaprClient } from "@dapr/dapr";
437437

438438
const daprHost = "127.0.0.1";
439-
const daprAppId = "example-config";
440439

441440
async function start() {
442-
const client = new DaprClient({ daprHost, daprPort: process.env.DAPR_HTTP_PORT });
441+
const client = new DaprClient({
442+
daprHost,
443+
daprPort: process.env.DAPR_GRPC_PORT,
444+
communicationProtocol: CommunicationProtocolEnum.GRPC,
445+
});
443446

444447
const config = await client.configuration.get("config-store", ["key1", "key2"]);
445448
console.log(config);
@@ -451,6 +454,58 @@ start().catch((e) => {
451454
});
452455
```
453456

457+
Sample output:
458+
459+
```log
460+
{
461+
items: {
462+
key1: { key: 'key1', value: 'foo', version: '', metadata: {} },
463+
key2: { key: 'key2', value: 'bar2', version: '', metadata: {} }
464+
}
465+
}
466+
```
467+
468+
#### Subscribe to Configuration Updates
469+
470+
```typescript
471+
import { DaprClient } from "@dapr/dapr";
472+
473+
const daprHost = "127.0.0.1";
474+
475+
async function start() {
476+
const client = new DaprClient({
477+
daprHost,
478+
daprPort: process.env.DAPR_GRPC_PORT,
479+
communicationProtocol: CommunicationProtocolEnum.GRPC,
480+
});
481+
482+
// Subscribes to config store changes for keys "key1" and "key2"
483+
const stream = await client.configuration.subscribeWithKeys("config-store", ["key1", "key2"], async (data) => {
484+
console.log("Subscribe received updates from config store: ", data);
485+
});
486+
487+
// Wait for 60 seconds and unsubscribe.
488+
await new Promise((resolve) => setTimeout(resolve, 60000));
489+
stream.stop();
490+
}
491+
492+
start().catch((e) => {
493+
console.error(e);
494+
process.exit(1);
495+
});
496+
```
497+
498+
Sample output:
499+
500+
```log
501+
Subscribe received updates from config store: {
502+
items: { key2: { key: 'key2', value: 'bar', version: '', metadata: {} } }
503+
}
504+
Subscribe received updates from config store: {
505+
items: { key1: { key: 'key1', value: 'foobar', version: '', metadata: {} } }
506+
}
507+
```
508+
454509
### Cryptography API
455510

456511
> Support for the cryptography API is only available on the gRPC client in the JavaScript SDK.

examples/configuration/README.md

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
# Examples - Configuration
22

3-
This example demonstrates how to use API Configuration.
3+
This example demonstrates how to use Configuration API.
4+
5+
## Prerequisites
6+
7+
In your Config store, create keys `key1` and `key2` to see the use of `get`.
8+
9+
```bash
10+
docker exec dapr_redis redis-cli MSET key1 "foo" key2 "bar"
11+
```
12+
13+
After the program starts, you can change the values of these keys to see use of `subscribe`.
14+
15+
```bash
16+
docker exec dapr_redis redis-cli MSET key2 "bar2"
17+
docker exec dapr_redis redis-cli MSET key1 "foobar"
18+
```
419

520
## Run
621

@@ -12,28 +27,25 @@ dapr init
1227
npm install
1328

1429
# Run the example directly using dapr run
15-
dapr run --app-id example-config --app-port 50051 --app-protocol http npm run start
30+
dapr run --app-id example-config --app-port 50051 --app-protocol grpc --resources-path ./components -- npm run start
1631

1732
# or, using npm script
18-
npm run start:dapr-http
33+
npm run start:dapr-grpc
1934
```
2035

21-
## Switching from HTTP to gRPC
22-
23-
By default, the example will run using HTTP. To use gRPC instead:
24-
25-
- Add `CommunicationProtocolEnum.GRPC` to the DaprClient object creation:
26-
27-
```typescript
28-
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.GRPC);
29-
```
36+
## Sample output
3037

31-
- To run:
32-
33-
```bash
34-
# Using dapr run
35-
dapr run --app-id example-config --app-port 50051 --app-protocol grpc npm run start
36-
37-
# or, using npm script
38-
npm run start:dapr-grpc
39-
```
38+
```
39+
{
40+
items: {
41+
key1: { key: 'key1', value: 'foo', version: '', metadata: {} },
42+
key2: { key: 'key2', value: 'bar2', version: '', metadata: {} }
43+
}
44+
}
45+
Subscribe received updates from config store: {
46+
items: { key2: { key: 'key2', value: 'bar', version: '', metadata: {} } }
47+
}
48+
Subscribe received updates from config store: {
49+
items: { key1: { key: 'key1', value: 'foobar', version: '', metadata: {} } }
50+
}
51+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: config-store
5+
spec:
6+
type: configuration.redis
7+
version: v1
8+
metadata:
9+
- name: redisHost
10+
value: localhost:6379
11+
- name: redisPassword
12+
value: ""

examples/configuration/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/configuration/src/index.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,33 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14-
import { DaprClient } from "@dapr/dapr";
14+
import { CommunicationProtocolEnum, DaprClient } from "@dapr/dapr";
1515

1616
const daprHost = "127.0.0.1";
17-
const daprPortDefault = "3500";
1817

1918
async function start() {
20-
const client = new DaprClient({ daprHost, daprPort: process.env.DAPR_HTTP_PORT ?? daprPortDefault });
19+
const client = new DaprClient({
20+
daprHost,
21+
daprPort: process.env.DAPR_GRPC_PORT,
22+
communicationProtocol: CommunicationProtocolEnum.GRPC,
23+
});
2124

25+
// Get keys from config store
2226
const config = await client.configuration.get("config-store", ["key1", "key2"]);
2327
console.log(config);
2428

25-
console.log(JSON.stringify(config));
29+
console.log("Subscribing to config store updates...");
30+
31+
// Subscribes to config store changes for keys "key1" and "key2"
32+
const stream = await client.configuration.subscribeWithKeys("config-store", ["key1", "key2"], async (data) => {
33+
console.log("Subscribe received updates from config store: ", data);
34+
});
35+
36+
// Wait for 60 seconds and unsubscribe.
37+
await new Promise((resolve) => setTimeout(resolve, 60000));
38+
stream.stop();
39+
40+
console.log("Unsubscribed from config store, exiting...");
2641
}
2742

2843
start().catch((e) => {

0 commit comments

Comments
 (0)