@@ -15,11 +15,13 @@ description: JavaScript Client SDK for developing Dapr applications
15
15
## Installing and importing Dapr's JS SDK
16
16
17
17
Install the SDK with npm:
18
- ```
18
+
19
+ ``` bash
19
20
npm i dapr-client
20
21
```
21
22
22
23
Import the libraries:
24
+
23
25
``` javascript
24
26
import { DaprClient , DaprServer , HttpMethod , CommunicationProtocolEnum } from " dapr-client" ;
25
27
@@ -37,11 +39,49 @@ const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, Commun
37
39
const client = new DaprClient (daprHost, daprPort, CommunicationProtocolEnum .GRPC );
38
40
```
39
41
40
- ##### DaprClient Library
42
+ ## Running
43
+
44
+ To run the examples, you can use two different protocols to interact with the Dapr sidecar: HTTP (default) or gRPC.
45
+
46
+ ### Using HTTP (default)
47
+
48
+ ``` javascript
49
+ import { DaprClient , DaprServer } from " dapr-client" ;
50
+ const client = new DaprClient (daprHost, daprPort);
51
+ const server = new DaprServer (appHost, appPort, daprHost, daprPort);
52
+ ```
53
+
54
+ ``` bash
55
+ # Using dapr run
56
+ dapr run --app-id < example-sdk> --app-port 50051 --app-protocol http npm run start
57
+
58
+ # or, using npm script
59
+ npm run start:dapr-http
60
+ ```
61
+
62
+ ### Using gRPC
63
+
64
+ 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.
65
+
66
+ ``` javascript
67
+ import { DaprClient , DaprServer , CommunicationProtocol } from " dapr-client" ;
68
+ const client = new DaprClient (daprHost, daprPort, CommunicationProtocol .GRPC );
69
+ const server = new DaprServer (appHost, appPort, daprHost, daprPort, CommunicationProtocol .GRPC );
70
+ ```
71
+
72
+ ``` bash
73
+ # Using dapr run
74
+ dapr run --app-id < example-sdk> --app-port 50051 --app-protocol grpc npm run start
75
+
76
+ # or, using npm script
77
+ npm run start:dapr-grpc
78
+ ```
79
+
80
+ ### DaprClient Library
41
81
A library that provides methods for how an application communicates with the Dapr sidecar.
42
82
43
- ##### DaprServer Library
44
- A library for how an application registers bindings / routes with Dapr. The ` start() ` method is used to start the server and bind the routes.
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.
45
85
46
86
## Building blocks
47
87
@@ -50,13 +90,13 @@ The JavaScript SDK allows you to interface with all of the [Dapr building blocks
50
90
### Invoke a service
51
91
52
92
``` javascript
53
- import { DaprClient , HttpMethod , CommunicationProtocolEnum } from " dapr-client" ;
93
+ import { DaprClient , HttpMethod } from " dapr-client" ;
54
94
55
95
const daprHost = " 127.0.0.1" ;
56
96
const daprPort = " 3500" ;
57
97
58
98
async function start () {
59
- const client = new DaprClient (daprHost, daprPort, CommunicationProtocolEnum . GRPC );
99
+ const client = new DaprClient (daprHost, daprPort);
60
100
61
101
const serviceAppId = " my-app-id" ;
62
102
const serviceMethod = " say-hello" ;
@@ -68,18 +108,19 @@ async function start() {
68
108
const response = await client .invoker .invoke (serviceAppId , serviceMethod , HttpMethod .GET );
69
109
}
70
110
```
111
+
71
112
- For a full guide on service invocation visit [ How-To: Invoke a service] ({{< ref howto-invoke-discover-services.md >}}).
72
113
73
114
### Save, get and delete application state
74
115
75
116
``` javascript
76
- import { DaprClient , CommunicationProtocolEnum } from " dapr-client" ;
117
+ import { DaprClient } from " dapr-client" ;
77
118
78
119
const daprHost = " 127.0.0.1" ;
79
120
const daprPort = " 3500" ;
80
121
81
122
async function start () {
82
- const client = new DaprClient (daprHost, daprPort, CommunicationProtocolEnum . GRPC );
123
+ const client = new DaprClient (daprHost, daprPort);
83
124
84
125
const serviceStoreName = " my-state-store-name" ;
85
126
@@ -122,20 +163,21 @@ async function start() {
122
163
const response = await client .state .delete (serviceStoreName, " first-key-name" );
123
164
}
124
165
```
166
+
125
167
- For a full list of state operations visit [ How-To: Get & save state] ({{< ref howto-get-save-state.md >}}).
126
168
127
169
### Publish & subscribe to messages
128
170
129
171
##### Publish messages
130
172
131
173
``` javascript
132
- import { DaprClient , CommunicationProtocolEnum } from " dapr-client" ;
174
+ import { DaprClient } from " dapr-client" ;
133
175
134
176
const daprHost = " 127.0.0.1" ;
135
177
const daprPort = " 3500" ;
136
178
137
179
async function start () {
138
- const client = new DaprClient (daprHost, daprPort, CommunicationProtocolEnum . GRPC );
180
+ const client = new DaprClient (daprHost, daprPort);
139
181
140
182
const pubSubName = " my-pubsub-name" ;
141
183
const topic = " topic-a" ;
@@ -149,15 +191,15 @@ async function start() {
149
191
##### Subscribe to messages
150
192
151
193
``` javascript
152
- import { DaprServer , CommunicationProtocolEnum } from " dapr-client" ;
194
+ import { DaprServer } from " dapr-client" ;
153
195
154
196
const daprHost = " 127.0.0.1" ; // Dapr Sidecar Host
155
197
const daprPort = " 3500" ; // Dapr Sidecar Port of this Example Server
156
198
const serverHost = " 127.0.0.1" ; // App Host of this Example Server
157
199
const serverPort = " 50051" ; // App Port of this Example Server "
158
200
159
201
async function start () {
160
- const server = new DaprServer (serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum . GRPC );
202
+ const server = new DaprServer (serverHost, serverPort, daprHost, daprPort);
161
203
162
204
const pubSubName = " my-pubsub-name" ;
163
205
const topic = " topic-a" ;
@@ -174,14 +216,15 @@ async function start() {
174
216
### Interact with bindings
175
217
176
218
** Output Bindings**
219
+
177
220
``` javascript
178
- import { DaprClient , CommunicationProtocolEnum } from " dapr-client" ;
221
+ import { DaprClient } from " dapr-client" ;
179
222
180
223
const daprHost = " 127.0.0.1" ;
181
224
const daprPort = " 3500" ;
182
225
183
226
async function start () {
184
- const client = new DaprClient (daprHost, daprPort, CommunicationProtocolEnum . GRPC );
227
+ const client = new DaprClient (daprHost, daprPort);
185
228
186
229
const bindingName = " my-binding-name" ;
187
230
const bindingOperation = " create" ;
@@ -192,16 +235,17 @@ async function start() {
192
235
```
193
236
194
237
** Input Bindings**
238
+
195
239
``` javascript
196
- import { DaprServer , CommunicationProtocolEnum } from " dapr-client" ;;
240
+ import { DaprServer } from " dapr-client" ;;
197
241
198
242
const daprHost = " 127.0.0.1" ;
199
243
const daprPort = " 3500" ;
200
244
const serverHost = " 127.0.0.1" ;
201
245
const serverPort = " 5051" ;
202
246
203
247
async function start () {
204
- const server = new DaprServer (serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum . GRPC );;
248
+ const server = new DaprServer (serverHost, serverPort, daprHost, daprPort);;
205
249
206
250
const bindingName = " my-binding-name" ;
207
251
@@ -216,13 +260,13 @@ async function start() {
216
260
### Retrieve secrets
217
261
218
262
``` javascript
219
- import { DaprClient , CommunicationProtocolEnum } from " dapr-client" ;
263
+ import { DaprClient } from " dapr-client" ;
220
264
221
265
const daprHost = " 127.0.0.1" ;
222
266
const daprPort = " 3500" ;
223
267
224
268
async function start () {
225
- const client = new DaprClient (daprHost, daprPort, CommunicationProtocolEnum . GRPC );
269
+ const client = new DaprClient (daprHost, daprPort);
226
270
227
271
const secretStoreName = " my-secret-store" ;
228
272
const secretKey = " secret-key" ;
@@ -234,7 +278,36 @@ async function start() {
234
278
const response = await client .secret .getBulk (secretStoreName);
235
279
}
236
280
```
281
+
237
282
- For a full guide on secrets visit [ How-To: Retrieve secrets] ({{< ref howto-secrets.md >}}).
238
283
284
+ ### Get configuration keys
285
+
286
+ ``` javascript
287
+ import { DaprClient } from " dapr-client" ;
288
+
289
+ const daprHost = " 127.0.0.1" ;
290
+ const daprAppId = " example-config" ;
291
+
292
+ async function start () {
293
+
294
+ const client = new DaprClient (
295
+ daprHost,
296
+ process .env .DAPR_HTTP_PORT
297
+ );
298
+
299
+ const config = await client .configuration .get (' config-store' , [' key1' , ' key2' ]);
300
+ console .log (config);
301
+
302
+ console .log (JSON .stringify (config));
303
+ }
304
+
305
+ start ().catch ((e ) => {
306
+ console .error (e);
307
+ process .exit (1 );
308
+ });
309
+ ```
310
+
239
311
## Related links
312
+
240
313
- [ JavaScript SDK examples] ( https://github.com/dapr/js-sdk/tree/master/examples )
0 commit comments