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