Skip to content

Commit

Permalink
Update examples and docs for subscribing to configuration stores (#524)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
shubham1172 authored Sep 21, 2023
1 parent bec157c commit 28367ef
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 34 deletions.
59 changes: 57 additions & 2 deletions daprdocs/content/en/js-sdk-docs/js-client/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,13 @@ start().catch((e) => {
import { DaprClient } from "@dapr/dapr";

const daprHost = "127.0.0.1";
const daprAppId = "example-config";

async function start() {
const client = new DaprClient({ daprHost, daprPort: process.env.DAPR_HTTP_PORT });
const client = new DaprClient({
daprHost,
daprPort: process.env.DAPR_GRPC_PORT,
communicationProtocol: CommunicationProtocolEnum.GRPC,
});

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

Sample output:

```log
{
items: {
key1: { key: 'key1', value: 'foo', version: '', metadata: {} },
key2: { key: 'key2', value: 'bar2', version: '', metadata: {} }
}
}
```

#### Subscribe to Configuration Updates

```typescript
import { DaprClient } from "@dapr/dapr";

const daprHost = "127.0.0.1";

async function start() {
const client = new DaprClient({
daprHost,
daprPort: process.env.DAPR_GRPC_PORT,
communicationProtocol: CommunicationProtocolEnum.GRPC,
});

// Subscribes to config store changes for keys "key1" and "key2"
const stream = await client.configuration.subscribeWithKeys("config-store", ["key1", "key2"], async (data) => {
console.log("Subscribe received updates from config store: ", data);
});

// Wait for 60 seconds and unsubscribe.
await new Promise((resolve) => setTimeout(resolve, 60000));
stream.stop();
}

start().catch((e) => {
console.error(e);
process.exit(1);
});
```

Sample output:

```log
Subscribe received updates from config store: {
items: { key2: { key: 'key2', value: 'bar', version: '', metadata: {} } }
}
Subscribe received updates from config store: {
items: { key1: { key: 'key1', value: 'foobar', version: '', metadata: {} } }
}
```

### Cryptography API

> Support for the cryptography API is only available on the gRPC client in the JavaScript SDK.
Expand Down
54 changes: 33 additions & 21 deletions examples/configuration/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
# Examples - Configuration

This example demonstrates how to use API Configuration.
This example demonstrates how to use Configuration API.

## Prerequisites

In your Config store, create keys `key1` and `key2` to see the use of `get`.

```bash
docker exec dapr_redis redis-cli MSET key1 "foo" key2 "bar"
```

After the program starts, you can change the values of these keys to see use of `subscribe`.

```bash
docker exec dapr_redis redis-cli MSET key2 "bar2"
docker exec dapr_redis redis-cli MSET key1 "foobar"
```

## Run

Expand All @@ -12,28 +27,25 @@ dapr init
npm install

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

# or, using npm script
npm run start:dapr-http
npm run start:dapr-grpc
```

## Switching from HTTP to gRPC

By default, the example will run using HTTP. To use gRPC instead:

- Add `CommunicationProtocolEnum.GRPC` to the DaprClient object creation:

```typescript
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.GRPC);
```
## Sample output

- To run:

```bash
# Using dapr run
dapr run --app-id example-config --app-port 50051 --app-protocol grpc npm run start

# or, using npm script
npm run start:dapr-grpc
```
```
{
items: {
key1: { key: 'key1', value: 'foo', version: '', metadata: {} },
key2: { key: 'key2', value: 'bar2', version: '', metadata: {} }
}
}
Subscribe received updates from config store: {
items: { key2: { key: 'key2', value: 'bar', version: '', metadata: {} } }
}
Subscribe received updates from config store: {
items: { key1: { key: 'key1', value: 'foobar', version: '', metadata: {} } }
}
```
12 changes: 12 additions & 0 deletions examples/configuration/components/redis-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: config-store
spec:
type: configuration.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
14 changes: 7 additions & 7 deletions examples/configuration/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 19 additions & 4 deletions examples/configuration/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,33 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { DaprClient } from "@dapr/dapr";
import { CommunicationProtocolEnum, DaprClient } from "@dapr/dapr";

const daprHost = "127.0.0.1";
const daprPortDefault = "3500";

async function start() {
const client = new DaprClient({ daprHost, daprPort: process.env.DAPR_HTTP_PORT ?? daprPortDefault });
const client = new DaprClient({
daprHost,
daprPort: process.env.DAPR_GRPC_PORT,
communicationProtocol: CommunicationProtocolEnum.GRPC,
});

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

console.log(JSON.stringify(config));
console.log("Subscribing to config store updates...");

// Subscribes to config store changes for keys "key1" and "key2"
const stream = await client.configuration.subscribeWithKeys("config-store", ["key1", "key2"], async (data) => {
console.log("Subscribe received updates from config store: ", data);
});

// Wait for 60 seconds and unsubscribe.
await new Promise((resolve) => setTimeout(resolve, 60000));
stream.stop();

console.log("Unsubscribed from config store, exiting...");
}

start().catch((e) => {
Expand Down

0 comments on commit 28367ef

Please sign in to comment.