@@ -10,22 +10,33 @@ description: JavaScript SDK packages for developing Dapr applications
10
10
11
11
- [ Dapr CLI] ({{< ref install-dapr-cli.md >}}) installed
12
12
- Initialized [ Dapr environment] ({{< ref install-dapr-selfhost.md >}})
13
- - [ Node version 15 or greater] ( https://nodejs.org/en/ )
13
+ - [ Latest LTS version of Node or greater] ( https://nodejs.org/en/ )
14
14
15
15
## Installing and importing Dapr's JS SDK
16
16
17
-
18
17
Install the SDK with npm:
19
18
```
20
- npm i @roadwork/ dapr- js-sdk
19
+ npm i @dapr/ js-sdk
21
20
```
22
21
23
- Import the libraries for the the given protocol you're using :
22
+ Import the libraries for the the given protocol:
24
23
25
24
``` javascript
26
- import { DaprClient , DaprServer } from " @roadwork/dapr-js-sdk/http" ;
27
- // OR (depending on the protocol)
28
- import { DaprClient , DaprServer } from " @roadwork/dapr-js-sdk/grpc" ;
25
+ const daprHost = " 127.0.0.1" ; // Dapr Sidecar Host
26
+ const daprPort = " 50050" ; // Dapr Sidecar Port of this Example Server
27
+ const daprPortActor = " 10002" ; // Dapr Sidecar Port of the Actor Server
28
+ const serverHost = " 127.0.0.1" ; // App Host of this Example Server
29
+ const serverPort = " 50051" ; // App Port of this Example Server
30
+
31
+ import { DaprClient , DaprServer , CommunicationProtocolEnum } from " @dapr/js-sdk" ;
32
+
33
+ // HTTP
34
+ const server = new DaprServer (serverHost, serverPort, daprHost, daprPort);
35
+ const client = new DaprClient (daprHost, daprPort);
36
+
37
+ // GRPC
38
+ const server = new DaprServer (serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum .GRPC );
39
+ const client = new DaprClient (daprHost, daprPort, CommunicationProtocolEnum .GRPC );
29
40
```
30
41
31
42
## Building blocks
@@ -35,60 +46,76 @@ The JavaScript SDK allows you to interface with all of the [Dapr building blocks
35
46
### Invoke a service
36
47
37
48
``` javascript
38
- import { DaprClient , HttpMethod } from " @roadwork/ dapr- js-sdk/http " ;
49
+ import { DaprClient , DaprServer } from " @dapr/ js-sdk" ;
39
50
40
- const daprHost = " 127.0.0.1" ;
51
+ const daprHost = " 127.0.0.1" ;
41
52
const daprPort = " 50050" ;
53
+ const serverHost = " 127.0.0.1" ;
54
+ const serverPort = " 50051" ;
42
55
43
56
async function start () {
44
- const client = new DaprClient (daprHost, daprPort);
57
+ const server = new DaprServer (serverHost, serverPort);
58
+
59
+ // POST listener
60
+ await server .invoker .listen (" hello-world" , async (data : any ) => {
61
+ return { hello: " world received from POST" };
62
+ }, { method: HttpMethod .POST });
45
63
64
+ // GET listener
65
+ await server .invoker .listen (" hello-world" , async () => {
66
+ return { hello: " world received from GET" };
67
+ }, { method: HttpMethod .GET });
68
+
69
+ // Start server after instantiating listeners
70
+ await server .startServer ();
71
+
72
+ const client = new DaprClient (daprHost, daprPort);
73
+
74
+ const serviceAppId = " my-dapr-app-id" ;
75
+ const serviceMethod = " say-hello" ;
76
+
46
77
// POST Request
47
- const response = await client .invoker .invoke (SERVICE_TO_INVOKE , METHOD_TO_INVOKE , HttpMethod .POST , {
48
- name: " World!"
49
- });
78
+ const response = await client .invoker .invoke (serviceAppId , serviceMethod , HttpMethod .POST , { hello: " world" });
50
79
51
80
// GET Request
52
- const response = await client .invoker .invoke (SERVICE_TO_INVOKE , METHOD_TO_INVOKE , HttpMethod .GET );
81
+ const response = await client .invoker .invoke (serviceAppId , serviceMethod , HttpMethod .GET );
53
82
}
54
83
```
55
84
- For a full guide on service invocation visit [ How-To: Invoke a service] ({{< ref howto-invoke-discover-services.md >}}).
56
85
57
86
### Save & get application state
58
87
59
88
``` javascript
60
- import { DaprClient } from " @roadwork/ dapr- js-sdk/http " ;
89
+ import { DaprClient } from " @dapr/ js-sdk" ;
61
90
62
- const daprHost = " 127.0.0.1" ;
91
+ const daprHost = " 127.0.0.1" ;
63
92
const daprPort = " 50050" ;
64
93
65
94
async function start () {
66
- const client = new DaprClient (daprHost, daprPort);
95
+ const client = new DaprClient (daprHost, daprPort);
96
+
97
+ const serviceStoreName = " my-dapr-state-store" ;
67
98
68
99
// Save state
69
- const response = await client .state .save (STATE_STORE_NAME , [
100
+ const response = await client .state .save (serviceStoreName , [
70
101
{
71
- key: FIRST_KEY_NAME
72
- value: FIRST_VALUE
102
+ key: " first-key-name " ,
103
+ value: " Hello "
73
104
},
74
105
{
75
- key: SECOND_KEY_NAME ,
76
- value: SECOND_VALUE
77
- },
78
- {
79
- key: THIRD_KEY_NAME
80
- value: THIRD_VALUE
106
+ key: " second-key-name" ,
107
+ value: " World!"
81
108
}
82
109
]);
83
110
84
111
// Get State
85
- const response = await client .state .get (STATE_STORE_NAME , FIRST_KEY_NAME );
112
+ const response = await client .state .get (serviceStoreName, " first-key-name " );
86
113
87
114
// Get Bulk State
88
- const response = await client .state .getBulk (STATE_STORE_NAME , [ FIRST_KEY_NAME , SECOND_KEY_NAME ]);
115
+ const response = await client .state .getBulk (serviceStoreName , [" first-key-name " , " second-key-name " ]);
89
116
90
117
// Delete State
91
- const response = await client .state .delete (STATE_STORE_NAME , FIRST_KEY_NAME );
118
+ const response = await client .state .delete (serviceStoreName, " first-key-name " );
92
119
}
93
120
```
94
121
- For a full list of state operations visit [ How-To: Get & save state] ({{< ref howto-get-save-state.md >}}).
@@ -98,30 +125,39 @@ async function start() {
98
125
##### Publish messages
99
126
100
127
``` javascript
101
- import { DaprClient } from " @roadwork/ dapr- js-sdk/http " ;
128
+ import { DaprClient } from " @dapr/ js-sdk" ;
102
129
103
- const daprHost = " 127.0.0.1" ;
130
+ const daprHost = " 127.0.0.1" ;
104
131
const daprPort = " 50050" ;
105
132
106
133
async function start () {
107
134
const client = new DaprClient (daprHost, daprPort);
108
135
109
- const response = await client .pubsub .publish (PUBSUB_NAME , TOPIC_NAME , { messsage });
136
+ const pubSubName = " my-dapr-pubsub" ;
137
+ const topic = " topic-a" ;
138
+ const message = { hello: " world" }
139
+
140
+ const response = await client .pubsub .publish (pubSubName- redis, topic, message);
110
141
}
111
142
```
112
143
113
144
##### Subscribe to messages
114
145
115
146
``` javascript
116
- import { DaprClient } from " @roadwork/ dapr- js-sdk/http " ;
147
+ import { DaprServer } from " @dapr/ js-sdk" ;
117
148
118
- const daprHost = " 127.0.0.1" ;
119
- const daprPort = " 50050 " ;
149
+ const serverHost = " 127.0.0.1" ;
150
+ const serverPort = " 50051 "
120
151
121
152
async function start () {
122
- const client = new DaprClient (daprHost, daprPort);
153
+ const server = new DaprServer (serverHost, serverPort);
154
+
155
+ const pubSubName = " my-dapr-pubsub" ;
156
+ const topic = " topic-a" ;
157
+
158
+ await server .pubsub .subscribe (pubSubName, topic, async (data : any ) => console .log (` Got Data: ${ JSON .stringify (data)} ` ));
123
159
124
- const response = await server .pubsub . subscribe ( PUBSUB_NAME , TOPIC_NAME , async ( data ) => console . log ( ` Got Data: ${ JSON . stringify (data) } ` ) );
160
+ await server .startServer ( );
125
161
}
126
162
```
127
163
@@ -131,30 +167,37 @@ async function start() {
131
167
132
168
** Output Bindings**
133
169
``` javascript
134
- import { DaprClient } from " @roadwork/ dapr- js-sdk/http " ;
170
+ import { DaprClient } from " @dapr/ js-sdk" ;
135
171
136
- const daprHost = " 127.0.0.1" ;
172
+ const daprHost = " 127.0.0.1" ;
137
173
const daprPort = " 50050" ;
138
174
139
175
async function start () {
140
176
const client = new DaprClient (daprHost, daprPort);
141
177
142
- const response = await client .binding .send (BINDING_NAME , BINDING_OPERATION , { message });
178
+ const bindingName = " my-binding-name" ;
179
+ const bindingOperation = " create" ;
180
+ const message = { hello: " world" };
181
+
182
+ const response = await client .binding .send (bindingName, bindingOperation, message);
143
183
}
144
184
```
145
185
146
186
** Input Bindings**
147
187
``` javascript
148
- import { DaprServer } from " @roadwork/ dapr- js-sdk/http " ;
188
+ import { DaprServer } from " @dapr/ js-sdk" ; ;
149
189
150
- const daprHost = " 127.0.0.1" ;
151
- const daprPort = " 50050" ;
152
- const daprInternalServerPort = " 50051" ; // App Port of this Example Server
190
+ const serverHost = " 127.0.0.1" ;
191
+ const serverPort = " 50051" ;
153
192
154
193
async function start () {
155
- const server = new DaprServer (daprHost, daprPort, daprInternalServerPort);
194
+ const server = new DaprServer (serverHost, serverPort);
195
+
196
+ const bindingName = " my-binding-name" ;
156
197
157
- const response = await server .binding .receive (BINDING_NAME , async (data ) => console .log (` Got Data: ${ JSON .stringify (data)} ` ));
198
+ const response = await server .binding .receive (bindingName, async (data : any ) => console .log (` Got Data: ${ JSON .stringify (data)} ` ));
199
+
200
+ await server .startServer ();
158
201
}
159
202
```
160
203
@@ -163,19 +206,22 @@ async function start() {
163
206
### Retrieve secrets
164
207
165
208
``` javascript
166
- import { DaprClient } from " @roadwork/ dapr- js-sdk/http " ;
209
+ import { DaprClient } from " @dapr/ js-sdk" ;
167
210
168
- const daprHost = " 127.0.0.1" ;
211
+ const daprHost = " 127.0.0.1" ;
169
212
const daprPort = " 50050" ;
170
213
171
214
async function start () {
172
215
const client = new DaprClient (daprHost, daprPort);
173
216
174
- // Retrieve a single secret from secret store
175
- const response = await client .secret .get (SECRET_STORE_NAME , secretKey);
217
+ const secretStoreName = " my-secret-store" ;
218
+ const secretKey = " secret-key" ;
219
+
220
+ // Retrieve a single secret from secret store
221
+ const response = await client .secret .get (secretStoreName, secretKey);
176
222
177
223
// Retrieve all secrets from secret store
178
- const response = await client .secret .getBulk (SECRET_STORE_NAME );
224
+ const response = await client .secret .getBulk (secretStoreName );
179
225
}
180
226
```
181
227
0 commit comments