Skip to content

Commit d579388

Browse files
Add documentation clarification
Signed-off-by: Xavier Geerinck <[email protected]>
1 parent 582a042 commit d579388

File tree

5 files changed

+329
-55
lines changed

5 files changed

+329
-55
lines changed

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,45 @@ no_list: true
99

1010
The Dapr JS SDK will allow you to interface with the Dapr process that abstracts several commonly used functionalities such as Service-to-Service invocation, State Management, PubSub, and more.
1111

12+
## Installation
13+
14+
To get started with the Javascript SDK, you can download the Dapr Javascript SDK package from [NPM](https://npmjs.org/package/dapr-client) by running the below:
15+
16+
```bash
17+
npm install --save dapr-client
18+
```
19+
20+
## Structure
21+
22+
The Dapr Javascript SDK exists out of 2 major components:
23+
24+
* **DaprServer:** The Dapr Server manages all communication from the Dapr Sidecar to your application
25+
* **DaprClient:** The Dapr Client manages all communication from your application to the Dapr Sidecar
26+
27+
To achieve communication, you are able to choose between 2 different protocols that are implemented: gRPC or HTTP
28+
29+
![](./js-server/dapr-server.jpg)
30+
![](./js-client/dapr-client.jpg)
31+
32+
## Get Started
33+
34+
To help you get started, feel free to check out the resources below:
1235

1336
<div class="card-deck">
1437
<div class="card">
1538
<div class="card-body">
1639
<h5 class="card-title"><b>Client</b></h5>
17-
<p class="card-text">Create a JavaScript client and interact with a Dapr sidecar and other Dapr applications.</p>
40+
<p class="card-text">Create a JavaScript client and interact with a Dapr sidecar and other Dapr applications. (e.g. Publishing events, Output Binding support, ...)</p>
1841
<a href="{{< ref js-client >}}" class="stretched-link"></a>
1942
</div>
2043
</div>
44+
<div class="card">
45+
<div class="card-body">
46+
<h5 class="card-title"><b>Server</b></h5>
47+
<p class="card-text">Create a JavaScript server and let the Dapr sidecar interact with your application. (e.g. Subscribing to events, Input Binding support, ...) </p>
48+
<a href="{{< ref js-server >}}" class="stretched-link"></a>
49+
</div>
50+
</div>
2151
<div class="card">
2252
<div class="card-body">
2353
<h5 class="card-title"><b>Actors</b></h5>

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

Lines changed: 108 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ weight: 500
66
description: JavaScript Client SDK for developing Dapr applications
77
---
88

9+
## Introduction
10+
11+
The Dapr Client will allow you to perform communication with the Dapr Sidecar and get access to its client facing features such as: Publishing Events, Invoking Output Bindings, State Management, Secret Management, and much more.
12+
913
## Pre-requisites
1014

1115
- [Dapr CLI]({{< ref install-dapr-cli.md >}}) installed
@@ -14,13 +18,13 @@ description: JavaScript Client SDK for developing Dapr applications
1418

1519
## Installing and importing Dapr's JS SDK
1620

17-
Install the SDK with npm:
21+
1. First install the SDK with `npm`:
1822

1923
```bash
2024
npm i dapr-client
2125
```
2226

23-
Import the libraries:
27+
2. Then, Import the libraries:
2428

2529
```javascript
2630
import { DaprClient, DaprServer, HttpMethod, CommunicationProtocolEnum } from "dapr-client";
@@ -30,12 +34,10 @@ const daprPort = "3500"; // Dapr Sidecar Port of this Example Server
3034
const serverHost = "127.0.0.1"; // App Host of this Example Server
3135
const serverPort = "50051"; // App Port of this Example Server
3236

33-
// HTTP
34-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
37+
// HTTP Example
3538
const client = new DaprClient(daprHost, daprPort);
3639

37-
// GRPC
38-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.GRPC);
40+
// GRPC Example
3941
const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
4042
```
4143

@@ -46,14 +48,13 @@ To run the examples, you can use two different protocols to interact with the Da
4648
### Using HTTP (default)
4749

4850
```javascript
49-
import { DaprClient, DaprServer } from "dapr-client";
51+
import { DaprClient } from "dapr-client";
5052
const client = new DaprClient(daprHost, daprPort);
51-
const server= new DaprServer(appHost, appPort, daprHost, daprPort);
5253
```
5354

5455
```bash
5556
# Using dapr run
56-
dapr run --app-id <example-sdk> --app-port 50051 --app-protocol http npm run start
57+
dapr run --app-id example-sdk --app-protocol http -- npm run start
5758

5859
# or, using npm script
5960
npm run start:dapr-http
@@ -64,30 +65,25 @@ npm run start:dapr-http
6465
Since HTTP is the default, you will have to adapt the communication protocol to use gRPC. You can do this by passing an extra argument to the client or server constructor.
6566

6667
```javascript
67-
import { DaprClient, DaprServer, CommunicationProtocol } from "dapr-client";
68+
import { DaprClient, CommunicationProtocol } from "dapr-client";
6869
const client = new DaprClient(daprHost, daprPort, CommunicationProtocol.GRPC);
69-
const server= new DaprServer(appHost, appPort, daprHost, daprPort, CommunicationProtocol.GRPC);
7070
```
7171

7272
```bash
7373
# Using dapr run
74-
dapr run --app-id <example-sdk> --app-port 50051 --app-protocol grpc npm run start
74+
dapr run --app-id example-sdk --app-protocol grpc -- npm run start
7575

7676
# or, using npm script
7777
npm run start:dapr-grpc
7878
```
7979

80-
### DaprClient Library
81-
A library that provides methods for how an application communicates with the Dapr sidecar.
82-
83-
### DaprServer Library
84-
A library for how an application registers bindings / routes with Dapr. The `start()` method is used to start the server and bind the routes.
85-
8680
## Building blocks
8781

88-
The JavaScript SDK allows you to interface with all of the [Dapr building blocks]({{< ref building-blocks >}}).
82+
The JavaScript Client SDK allows you to interface with all of the [Dapr building blocks]({{< ref building-blocks >}}) focusing on Client to Sidecar features.
8983

90-
### Invoke a service
84+
### Invocation API
85+
86+
#### Invoke a Service
9187

9288
```javascript
9389
import { DaprClient, HttpMethod } from "dapr-client";
@@ -107,11 +103,19 @@ async function start() {
107103
// GET Request
108104
const response = await client.invoker.invoke(serviceAppId , serviceMethod , HttpMethod.GET);
109105
}
106+
107+
start().catch((e) => {
108+
console.error(e);
109+
process.exit(1);
110+
});
110111
```
111112

112-
- For a full guide on service invocation visit [How-To: Invoke a service]({{< ref howto-invoke-discover-services.md >}}).
113+
> For a full guide on service invocation visit [How-To: Invoke a service]({{< ref howto-invoke-discover-services.md >}}).
114+
115+
116+
### State Management API
113117

114-
### Save, get and delete application state
118+
#### Save, Get and Delete application state
115119

116120
```javascript
117121
import { DaprClient } from "dapr-client";
@@ -162,13 +166,65 @@ async function start() {
162166
// Delete State
163167
const response = await client.state.delete(serviceStoreName, "first-key-name");
164168
}
169+
170+
start().catch((e) => {
171+
console.error(e);
172+
process.exit(1);
173+
});
165174
```
166175

167-
- For a full list of state operations visit [How-To: Get & save state]({{< ref howto-get-save-state.md >}}).
176+
> For a full list of state operations visit [How-To: Get & save state]({{< ref howto-get-save-state.md >}}).
177+
178+
179+
#### Query State API
180+
181+
```javascript
182+
import { DaprClient } from "dapr-client";
183+
184+
async function start() {
185+
const client = new DaprClient(daprHost, daprPort);
186+
187+
const res = await client.state.query("state-mongodb", {
188+
filter: {
189+
OR: [
190+
{
191+
EQ: { "person.org": "Dev Ops" }
192+
},
193+
{
194+
"AND": [
195+
{
196+
"EQ": { "person.org": "Finance" }
197+
},
198+
{
199+
"IN": { "state": ["CA", "WA"] }
200+
}
201+
]
202+
}
203+
]
204+
},
205+
sort: [
206+
{
207+
key: "state",
208+
order: "DESC"
209+
}
210+
],
211+
page: {
212+
limit: 10
213+
}
214+
});
215+
216+
console.log(res);
217+
}
218+
219+
start().catch((e) => {
220+
console.error(e);
221+
process.exit(1);
222+
});
223+
```
168224

169-
### Publish & subscribe to messages
225+
### PubSub API
170226

171-
##### Publish messages
227+
#### Publish messages
172228

173229
```javascript
174230
import { DaprClient } from "dapr-client";
@@ -186,6 +242,11 @@ async function start() {
186242
// Publish Message to Topic
187243
const response = await client.pubsub.publish(pubSubName, topic, message);
188244
}
245+
246+
start().catch((e) => {
247+
console.error(e);
248+
process.exit(1);
249+
});
189250
```
190251

191252
##### Subscribe to messages
@@ -211,9 +272,11 @@ async function start() {
211272
}
212273
```
213274

214-
- For a full list of state operations visit [How-To: Publish & subscribe]({{< ref howto-publish-subscribe.md >}}).
275+
> For a full list of state operations visit [How-To: Publish & subscribe]({{< ref howto-publish-subscribe.md >}}).
215276
216-
### Interact with bindings
277+
### Bindings API
278+
279+
#### Invoke Output Binding
217280

218281
**Output Bindings**
219282

@@ -232,32 +295,18 @@ async function start() {
232295

233296
const response = await client.binding.send(bindingName, bindingOperation, message);
234297
}
235-
```
236-
237-
**Input Bindings**
238-
239-
```javascript
240-
import { DaprServer } from "dapr-client";;
241-
242-
const daprHost = "127.0.0.1";
243-
const daprPort = "3500";
244-
const serverHost = "127.0.0.1";
245-
const serverPort = "5051";
246-
247-
async function start() {
248-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);;
249298

250-
const bindingName = "my-binding-name";
251-
252-
const response = await server.binding.receive(bindingName, async (data: any) => console.log(`Got Data: ${JSON.stringify(data)}`));
253-
254-
await server.start();
255-
}
299+
start().catch((e) => {
300+
console.error(e);
301+
process.exit(1);
302+
});
256303
```
257304

258-
- For a full guide on output bindings visit [How-To: Use bindings]({{< ref howto-bindings.md >}}).
305+
> For a full guide on output bindings visit [How-To: Use bindings]({{< ref howto-bindings.md >}}).
259306
260-
### Retrieve secrets
307+
### Secret API
308+
309+
#### Retrieve secrets
261310

262311
```javascript
263312
import { DaprClient } from "dapr-client";
@@ -277,11 +326,18 @@ async function start() {
277326
// Retrieve all secrets from secret store
278327
const response = await client.secret.getBulk(secretStoreName);
279328
}
329+
330+
start().catch((e) => {
331+
console.error(e);
332+
process.exit(1);
333+
});
280334
```
281335

282-
- For a full guide on secrets visit [How-To: Retrieve secrets]({{< ref howto-secrets.md >}}).
336+
> For a full guide on secrets visit [How-To: Retrieve secrets]({{< ref howto-secrets.md >}}).
283337
284-
### Get configuration keys
338+
### Configuration API
339+
340+
#### Get Configuration Keys
285341

286342
```javascript
287343
import { DaprClient } from "dapr-client";
@@ -297,9 +353,7 @@ async function start() {
297353
);
298354

299355
const config = await client.configuration.get('config-store', ['key1', 'key2']);
300-
console.log(config);
301-
302-
console.log(JSON.stringify(config));
356+
console.log(config);
303357
}
304358

305359
start().catch((e) => {
215 KB
Loading

0 commit comments

Comments
 (0)