|
1 | 1 | ---
|
2 | 2 | type: docs
|
3 |
| -title: "Dapr JavaScript SDK" |
| 3 | +title: "JavaScript SDK" |
4 | 4 | linkTitle: "JavaScript"
|
5 | 5 | weight: 1000
|
6 | 6 | description: JavaScript SDK packages for developing Dapr applications
|
| 7 | +no_list: true |
7 | 8 | ---
|
8 |
| - |
9 |
| -## Pre-requisites |
10 |
| - |
11 |
| -- [Dapr CLI]({{< ref install-dapr-cli.md >}}) installed |
12 |
| -- Initialized [Dapr environment]({{< ref install-dapr-selfhost.md >}}) |
13 |
| -- [Latest LTS version of Node or greater](https://nodejs.org/en/) |
14 |
| - |
15 |
| -## Installing and importing Dapr's JS SDK |
16 |
| - |
17 |
| -Install the SDK with npm: |
18 |
| -``` |
19 |
| -npm i @dapr/js-sdk |
20 |
| -``` |
21 |
| - |
22 |
| -Import the libraries for the the given protocol: |
23 |
| -```javascript |
24 |
| -const daprHost = "127.0.0.1"; // Dapr Sidecar Host |
25 |
| -const daprPort = "50050"; // Dapr Sidecar Port of this Example Server |
26 |
| -const daprPortActor = "10002"; // Dapr Sidecar Port of the Actor Server |
27 |
| -const serverHost = "127.0.0.1"; // App Host of this Example Server |
28 |
| -const serverPort = "50051"; // App Port of this Example Server |
29 |
| - |
30 |
| -import { DaprClient, DaprServer, CommunicationProtocolEnum } from "@dapr/js-sdk"; |
31 |
| - |
32 |
| -// HTTP |
33 |
| -const server = new DaprServer(serverHost, serverPort, daprHost, daprPort); |
34 |
| -const client = new DaprClient(daprHost, daprPort); |
35 |
| - |
36 |
| -// GRPC |
37 |
| -const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.GRPC); |
38 |
| -const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC); |
39 |
| -``` |
40 |
| - |
41 |
| -##### Client Library |
42 |
| -A library that provides methods for how an application communicates with the Dapr sidecar. |
43 |
| - |
44 |
| -##### Server Library |
45 |
| -A library for how an application registers bindings / routes with Dapr. The `startServer` method is used to start the server and bind the routes. |
46 |
| - |
47 |
| - |
48 |
| -## Building blocks |
49 |
| - |
50 |
| -The JavaScript SDK allows you to interface with all of the [Dapr building blocks]({{< ref building-blocks >}}). |
51 |
| - |
52 |
| -### Invoke a service |
53 |
| - |
54 |
| -```javascript |
55 |
| -import { DaprClient } from "@dapr/js-sdk"; |
56 |
| - |
57 |
| -const daprHost = "127.0.0.1"; |
58 |
| -const daprPort = "50050"; |
59 |
| - |
60 |
| -async function start() { |
61 |
| - const client = new DaprClient(daprHost, daprPort); |
62 |
| - |
63 |
| - const serviceAppId = "my-dapr-app-id"; |
64 |
| - const serviceMethod = "say-hello"; |
65 |
| - |
66 |
| - // POST Request |
67 |
| - const response = await client.invoker.invoke(serviceAppId , serviceMethod , HttpMethod.POST, { hello: "world" }); |
68 |
| - |
69 |
| - // GET Request |
70 |
| - const response = await client.invoker.invoke(serviceAppId , serviceMethod , HttpMethod.GET); |
71 |
| -} |
72 |
| -``` |
73 |
| -- For a full guide on service invocation visit [How-To: Invoke a service]({{< ref howto-invoke-discover-services.md >}}). |
74 |
| - |
75 |
| -### Save, get and delete application state |
76 |
| - |
77 |
| -```javascript |
78 |
| -import { DaprClient } from "@dapr/js-sdk"; |
79 |
| - |
80 |
| -const daprHost = "127.0.0.1"; |
81 |
| -const daprPort = "50050"; |
82 |
| - |
83 |
| -async function start() { |
84 |
| - const client = new DaprClient(daprHost, daprPort); |
85 |
| - |
86 |
| - const serviceStoreName = "my-dapr-state-store"; |
87 |
| - |
88 |
| - // Save State |
89 |
| - const response = await client.state.save(serviceStoreName, [ |
90 |
| - { |
91 |
| - key: "first-key-name", |
92 |
| - value: "hello" |
93 |
| - }, |
94 |
| - { |
95 |
| - key: "second-key-name", |
96 |
| - value: "world" |
97 |
| - } |
98 |
| - ]); |
99 |
| - |
100 |
| - // Get State |
101 |
| - const response = await client.state.get(serviceStoreName, "first-key-name"); |
102 |
| - |
103 |
| - // Get Bulk State |
104 |
| - const response = await client.state.getBulk(serviceStoreName, ["first-key-name", "second-key-name"]); |
105 |
| - |
106 |
| - // Delete State |
107 |
| - const response = await client.state.delete(serviceStoreName, "first-key-name"); |
108 |
| -} |
109 |
| -``` |
110 |
| -- For a full list of state operations visit [How-To: Get & save state]({{< ref howto-get-save-state.md >}}). |
111 |
| - |
112 |
| -### Publish & subscribe to messages |
113 |
| - |
114 |
| -##### Publish messages |
115 |
| - |
116 |
| -```javascript |
117 |
| -import { DaprClient } from "@dapr/js-sdk"; |
118 |
| - |
119 |
| -const daprHost = "127.0.0.1"; |
120 |
| -const daprPort = "50050"; |
121 |
| - |
122 |
| -async function start() { |
123 |
| - const client = new DaprClient(daprHost, daprPort); |
124 |
| - |
125 |
| - const pubSubName = "my-dapr-pubsub"; |
126 |
| - const topic = "topic-a"; |
127 |
| - const message = { hello: "world" } |
128 |
| - |
129 |
| - // Publish Message to Topic |
130 |
| - const response = await client.pubsub.publish(pubSubName, topic, message); |
131 |
| -} |
132 |
| -``` |
133 |
| - |
134 |
| -##### Subscribe to messages |
135 |
| - |
136 |
| -```javascript |
137 |
| -import { DaprServer } from "@dapr/js-sdk"; |
138 |
| - |
139 |
| -const serverHost = "127.0.0.1"; |
140 |
| -const serverPort = "50051" |
141 |
| - |
142 |
| -async function start() { |
143 |
| - const server = new DaprServer(serverHost, serverPort); |
144 |
| - |
145 |
| - const pubSubName = "my-dapr-pubsub"; |
146 |
| - const topic = "topic-a"; |
147 |
| - |
148 |
| - // Configure Subscriber for a Topic |
149 |
| - await server.pubsub.subscribe(pubSubName, topic, async (data: any) => console.log(`Got Data: ${JSON.stringify(data)}`)); |
150 |
| - |
151 |
| - await server.startServer(); |
152 |
| -} |
153 |
| -``` |
154 |
| - |
155 |
| -- For a full list of state operations visit [How-To: Publish & subscribe]({{< ref howto-publish-subscribe.md >}}). |
156 |
| - |
157 |
| -### Interact with bindings |
158 |
| - |
159 |
| -**Output Bindings** |
160 |
| -```javascript |
161 |
| -import { DaprClient } from "@dapr/js-sdk"; |
162 |
| - |
163 |
| -const daprHost = "127.0.0.1"; |
164 |
| -const daprPort = "50050"; |
165 |
| - |
166 |
| -async function start() { |
167 |
| - const client = new DaprClient(daprHost, daprPort); |
168 |
| - |
169 |
| - const bindingName = "my-binding-name"; |
170 |
| - const bindingOperation = "create"; |
171 |
| - const message = { hello: "world" }; |
172 |
| - |
173 |
| - const response = await client.binding.send(bindingName, bindingOperation, message); |
174 |
| -} |
175 |
| -``` |
176 |
| - |
177 |
| -**Input Bindings** |
178 |
| -```javascript |
179 |
| -import { DaprServer } from "@dapr/js-sdk";; |
180 |
| - |
181 |
| -const serverHost = "127.0.0.1"; |
182 |
| -const serverPort = "50051"; |
183 |
| - |
184 |
| -async function start() { |
185 |
| - const server = new DaprServer(serverHost, serverPort); |
186 |
| - |
187 |
| - const bindingName = "my-binding-name"; |
188 |
| - |
189 |
| - const response = await server.binding.receive(bindingName, async (data: any) => console.log(`Got Data: ${JSON.stringify(data)}`)); |
190 |
| - |
191 |
| - await server.startServer(); |
192 |
| -} |
193 |
| -``` |
194 |
| - |
195 |
| -- For a full guide on output bindings visit [How-To: Use bindings]({{< ref howto-bindings.md >}}). |
196 |
| - |
197 |
| -### Retrieve secrets |
198 |
| - |
199 |
| -```javascript |
200 |
| -import { DaprClient } from "@dapr/js-sdk"; |
201 |
| - |
202 |
| -const daprHost = "127.0.0.1"; |
203 |
| -const daprPort = "50050"; |
204 |
| - |
205 |
| -async function start() { |
206 |
| - const client = new DaprClient(daprHost, daprPort); |
207 |
| - |
208 |
| - const secretStoreName = "my-secret-store"; |
209 |
| - const secretKey = "secret-key"; |
210 |
| - |
211 |
| - // Retrieve a single secret from secret store |
212 |
| - const response = await client.secret.get(secretStoreName, secretKey); |
213 |
| - |
214 |
| - // Retrieve all secrets from secret store |
215 |
| - const response = await client.secret.getBulk(secretStoreName); |
216 |
| -} |
217 |
| -``` |
218 |
| - |
219 |
| -- For a full guide on secrets visit [How-To: Retrieve secrets]({{< ref howto-secrets.md >}}). |
220 |
| - |
221 |
| -### Actors |
222 |
| - |
223 |
| -```javascript |
224 |
| -``` |
225 |
| - |
226 |
| -- For a full guide on actors visit [How-To: Use virtual actors in Dapr]({{< ref howto-actors.md >}}). |
227 |
| - |
228 |
| -## Related links |
229 |
| -- [JavaScript SDK examples](https://github.com/dapr/js-sdk/tree/master/examples) |
| 9 | +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. |
| 10 | + |
| 11 | +<div class="card-deck"> |
| 12 | + <div class="card"> |
| 13 | + <div class="card-body"> |
| 14 | + <h5 class="card-title"><b>Client</b></h5> |
| 15 | + <p class="card-text">Create a JavaScript client and interact with a Dapr sidecar and other Dapr applications.</p> |
| 16 | + <a href="{{< ref js-client >}}" class="stretched-link"></a> |
| 17 | + </div> |
| 18 | + </div> |
| 19 | + <div class="card"> |
| 20 | + <div class="card-body"> |
| 21 | + <h5 class="card-title"><b>Actors</b></h5> |
| 22 | + <p class="card-text">Create virtual actors with state, reminders/timers, and methods in JavaScript.</p> |
| 23 | + <a href="{{< ref js-actors >}}" class="stretched-link"></a> |
| 24 | + </div> |
| 25 | + </div> |
| 26 | + <div class="card"> |
| 27 | + <div class="card-body"> |
| 28 | + <h5 class="card-title"><b>Examples</b></h5> |
| 29 | + <p class="card-text">Clone the JavaScript SDK repo and try out some of the examples and get started quickly.</p> |
| 30 | + <a href="https://github.com/dapr/js-sdk/blob/master/documentation/examples.md" class="stretched-link"></a> |
| 31 | + </div> |
| 32 | + </div> |
| 33 | +</div> |
0 commit comments