@@ -15,11 +15,13 @@ description: JavaScript Client SDK for developing Dapr applications
1515## Installing and importing Dapr's JS SDK
1616
1717Install the SDK with npm:
18- ```
18+
19+ ``` bash
1920npm i dapr-client
2021```
2122
2223Import the libraries:
24+
2325``` javascript
2426import { DaprClient , DaprServer , HttpMethod , CommunicationProtocolEnum } from " dapr-client" ;
2527
@@ -37,11 +39,49 @@ const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, Commun
3739const client = new DaprClient (daprHost, daprPort, CommunicationProtocolEnum .GRPC );
3840```
3941
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
4181A library that provides methods for how an application communicates with the Dapr sidecar.
4282
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.
4585
4686## Building blocks
4787
@@ -50,13 +90,13 @@ The JavaScript SDK allows you to interface with all of the [Dapr building blocks
5090### Invoke a service
5191
5292``` javascript
53- import { DaprClient , HttpMethod , CommunicationProtocolEnum } from " dapr-client" ;
93+ import { DaprClient , HttpMethod } from " dapr-client" ;
5494
5595const daprHost = " 127.0.0.1" ;
5696const daprPort = " 3500" ;
5797
5898async function start () {
59- const client = new DaprClient (daprHost, daprPort, CommunicationProtocolEnum . GRPC );
99+ const client = new DaprClient (daprHost, daprPort);
60100
61101 const serviceAppId = " my-app-id" ;
62102 const serviceMethod = " say-hello" ;
@@ -68,18 +108,19 @@ async function start() {
68108 const response = await client .invoker .invoke (serviceAppId , serviceMethod , HttpMethod .GET );
69109}
70110```
111+
71112- For a full guide on service invocation visit [ How-To: Invoke a service] ({{< ref howto-invoke-discover-services.md >}}).
72113
73114### Save, get and delete application state
74115
75116``` javascript
76- import { DaprClient , CommunicationProtocolEnum } from " dapr-client" ;
117+ import { DaprClient } from " dapr-client" ;
77118
78119const daprHost = " 127.0.0.1" ;
79120const daprPort = " 3500" ;
80121
81122async function start () {
82- const client = new DaprClient (daprHost, daprPort, CommunicationProtocolEnum . GRPC );
123+ const client = new DaprClient (daprHost, daprPort);
83124
84125 const serviceStoreName = " my-state-store-name" ;
85126
@@ -122,20 +163,21 @@ async function start() {
122163 const response = await client .state .delete (serviceStoreName, " first-key-name" );
123164}
124165```
166+
125167- For a full list of state operations visit [ How-To: Get & save state] ({{< ref howto-get-save-state.md >}}).
126168
127169### Publish & subscribe to messages
128170
129171##### Publish messages
130172
131173``` javascript
132- import { DaprClient , CommunicationProtocolEnum } from " dapr-client" ;
174+ import { DaprClient } from " dapr-client" ;
133175
134176const daprHost = " 127.0.0.1" ;
135177const daprPort = " 3500" ;
136178
137179async function start () {
138- const client = new DaprClient (daprHost, daprPort, CommunicationProtocolEnum . GRPC );
180+ const client = new DaprClient (daprHost, daprPort);
139181
140182 const pubSubName = " my-pubsub-name" ;
141183 const topic = " topic-a" ;
@@ -149,15 +191,15 @@ async function start() {
149191##### Subscribe to messages
150192
151193``` javascript
152- import { DaprServer , CommunicationProtocolEnum } from " dapr-client" ;
194+ import { DaprServer } from " dapr-client" ;
153195
154196const daprHost = " 127.0.0.1" ; // Dapr Sidecar Host
155197const daprPort = " 3500" ; // Dapr Sidecar Port of this Example Server
156198const serverHost = " 127.0.0.1" ; // App Host of this Example Server
157199const serverPort = " 50051" ; // App Port of this Example Server "
158200
159201async function start () {
160- const server = new DaprServer (serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum . GRPC );
202+ const server = new DaprServer (serverHost, serverPort, daprHost, daprPort);
161203
162204 const pubSubName = " my-pubsub-name" ;
163205 const topic = " topic-a" ;
@@ -174,14 +216,15 @@ async function start() {
174216### Interact with bindings
175217
176218** Output Bindings**
219+
177220``` javascript
178- import { DaprClient , CommunicationProtocolEnum } from " dapr-client" ;
221+ import { DaprClient } from " dapr-client" ;
179222
180223const daprHost = " 127.0.0.1" ;
181224const daprPort = " 3500" ;
182225
183226async function start () {
184- const client = new DaprClient (daprHost, daprPort, CommunicationProtocolEnum . GRPC );
227+ const client = new DaprClient (daprHost, daprPort);
185228
186229 const bindingName = " my-binding-name" ;
187230 const bindingOperation = " create" ;
@@ -192,16 +235,17 @@ async function start() {
192235```
193236
194237** Input Bindings**
238+
195239``` javascript
196- import { DaprServer , CommunicationProtocolEnum } from " dapr-client" ;;
240+ import { DaprServer } from " dapr-client" ;;
197241
198242const daprHost = " 127.0.0.1" ;
199243const daprPort = " 3500" ;
200244const serverHost = " 127.0.0.1" ;
201245const serverPort = " 5051" ;
202246
203247async function start () {
204- const server = new DaprServer (serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum . GRPC );;
248+ const server = new DaprServer (serverHost, serverPort, daprHost, daprPort);;
205249
206250 const bindingName = " my-binding-name" ;
207251
@@ -216,13 +260,13 @@ async function start() {
216260### Retrieve secrets
217261
218262``` javascript
219- import { DaprClient , CommunicationProtocolEnum } from " dapr-client" ;
263+ import { DaprClient } from " dapr-client" ;
220264
221265const daprHost = " 127.0.0.1" ;
222266const daprPort = " 3500" ;
223267
224268async function start () {
225- const client = new DaprClient (daprHost, daprPort, CommunicationProtocolEnum . GRPC );
269+ const client = new DaprClient (daprHost, daprPort);
226270
227271 const secretStoreName = " my-secret-store" ;
228272 const secretKey = " secret-key" ;
@@ -234,7 +278,36 @@ async function start() {
234278 const response = await client .secret .getBulk (secretStoreName);
235279}
236280```
281+
237282- For a full guide on secrets visit [ How-To: Retrieve secrets] ({{< ref howto-secrets.md >}}).
238283
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+
239311## Related links
312+
240313- [ JavaScript SDK examples] ( https://github.com/dapr/js-sdk/tree/master/examples )
0 commit comments