|
| 1 | +--- |
| 2 | +type: docs |
| 3 | +title: "Dapr JavaScript SDK" |
| 4 | +linkTitle: "JavaScript" |
| 5 | +weight: 1000 |
| 6 | +description: JavaScript SDK packages 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 | +- [Node version 15 or greater](https://nodejs.org/en/) |
| 14 | + |
| 15 | +## Installing and importing Dapr's JS SDK |
| 16 | + |
| 17 | + |
| 18 | +Install the SDK with npm: |
| 19 | +``` |
| 20 | +npm i @roadwork/dapr-js-sdk |
| 21 | +``` |
| 22 | + |
| 23 | +Import the libraries for the the given protocol you're using: |
| 24 | + |
| 25 | +```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"; |
| 29 | +``` |
| 30 | + |
| 31 | +## Building blocks |
| 32 | + |
| 33 | +The JavaScript SDK allows you to interface with all of the [Dapr building blocks]({{< ref building-blocks >}}). |
| 34 | + |
| 35 | +### Invoke a service |
| 36 | + |
| 37 | +```javascript |
| 38 | +import { DaprClient, HttpMethod } from "@roadwork/dapr-js-sdk/http"; |
| 39 | + |
| 40 | +const daprHost = "127.0.0.1"; |
| 41 | +const daprPort = "50050"; |
| 42 | + |
| 43 | +async function start() { |
| 44 | + const client = new DaprClient(daprHost, daprPort); |
| 45 | + |
| 46 | + //POST Request |
| 47 | + const response = await client.invoker.invoke(SERVICE_TO_INVOKE, METHOD_TO_INVOKE, HttpMethod.POST, { |
| 48 | + name: "World!" |
| 49 | + }); |
| 50 | + |
| 51 | + //GET Request |
| 52 | + const response = await client.invoker.invoke(SERVICE_TO_INVOKE, METHOD_TO_INVOKE, HttpMethod.GET); |
| 53 | +} |
| 54 | +``` |
| 55 | +- For a full guide on service invocation visit [How-To: Invoke a service]({{< ref howto-invoke-discover-services.md >}}). |
| 56 | + |
| 57 | +### Save & get application state |
| 58 | + |
| 59 | +```javascript |
| 60 | +import { DaprClient } from "@roadwork/dapr-js-sdk/http"; |
| 61 | + |
| 62 | +const daprHost = "127.0.0.1"; |
| 63 | +const daprPort = "50050"; |
| 64 | + |
| 65 | +async function start() { |
| 66 | + const client = new DaprClient(daprHost, daprPort); |
| 67 | + |
| 68 | + //Save state |
| 69 | + const response = await client.state.save(STATE_STORE_NAME, [ |
| 70 | + { |
| 71 | + key: FIRST_KEY_NAME |
| 72 | + value: FIRST_VALUE |
| 73 | + }, |
| 74 | + { |
| 75 | + key: SECOND_KEY_NAME, |
| 76 | + value: SECOND_VALUE |
| 77 | + }, |
| 78 | + { |
| 79 | + key: THIRD_KEY_NAME |
| 80 | + value: THIRD_VALUE |
| 81 | + } |
| 82 | + ]); |
| 83 | + |
| 84 | + //Get State |
| 85 | + const response = await client.state.get(STATE_STORE_NAME, FIRST_KEY_NAME); |
| 86 | + |
| 87 | + //Get Bulk State |
| 88 | + const response = await client.state.getBulk(STATE_STORE_NAME, [ FIRST_KEY_NAME, SECOND_KEY_NAME ]); |
| 89 | + |
| 90 | + //Delete State |
| 91 | + const response = await client.state.delete(STATE_STORE_NAME, FIRST_KEY_NAME); |
| 92 | +} |
| 93 | +``` |
| 94 | +- For a full list of state operations visit [How-To: Get & save state]({{< ref howto-get-save-state.md >}}). |
| 95 | + |
| 96 | +### Publish & subscribe to messages |
| 97 | + |
| 98 | +##### Publish messages |
| 99 | + |
| 100 | +```javascript |
| 101 | +import { DaprClient } from "@roadwork/dapr-js-sdk/http"; |
| 102 | + |
| 103 | +const daprHost = "127.0.0.1"; |
| 104 | +const daprPort = "50050"; |
| 105 | + |
| 106 | +async function start() { |
| 107 | + const client = new DaprClient(daprHost, daprPort); |
| 108 | + |
| 109 | + const response = await client.pubsub.publish(PUBSUB_NAME, TOPIC_NAME, { messsage }); |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +##### Subscribe to messages |
| 114 | + |
| 115 | +```javascript |
| 116 | +import { DaprClient } from "@roadwork/dapr-js-sdk/http"; |
| 117 | + |
| 118 | +const daprHost = "127.0.0.1"; |
| 119 | +const daprPort = "50050"; |
| 120 | + |
| 121 | +async function start() { |
| 122 | + const client = new DaprClient(daprHost, daprPort); |
| 123 | + |
| 124 | + const response = await server.pubsub.subscribe(PUBSUB_NAME, TOPIC_NAME, async (data) => console.log(`Got Data: ${JSON.stringify(data)}`)); |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +- For a full list of state operations visit [How-To: Publish & subscribe]({{< ref howto-publish-subscribe.md >}}). |
| 129 | + |
| 130 | +### Interact with bindings |
| 131 | + |
| 132 | +**Output Bindings** |
| 133 | +```javascript |
| 134 | +import { DaprClient } from "@roadwork/dapr-js-sdk/http"; |
| 135 | + |
| 136 | +const daprHost = "127.0.0.1"; |
| 137 | +const daprPort = "50050"; |
| 138 | + |
| 139 | +async function start() { |
| 140 | + const client = new DaprClient(daprHost, daprPort); |
| 141 | + |
| 142 | + const response = await client.binding.send(BINDING_NAME, BINDING_OPERATION, { message }); |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +**Input Bindings** |
| 147 | +```javascript |
| 148 | +import { DaprServer } from "@roadwork/dapr-js-sdk/http"; |
| 149 | + |
| 150 | +const daprHost = "127.0.0.1"; |
| 151 | +const daprPort = "50050"; |
| 152 | +const daprInternalServerPort = "50051"; // App Port of this Example Server |
| 153 | + |
| 154 | +async function start() { |
| 155 | + const server = new DaprServer(daprHost, daprPort, daprInternalServerPort); |
| 156 | + |
| 157 | + const response = await server.binding.receive(BINDING_NAME, async (data) => console.log(`Got Data: ${JSON.stringify(data)}`)); |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +- For a full guide on output bindings visit [How-To: Use bindings]({{< ref howto-bindings.md >}}). |
| 162 | + |
| 163 | +### Retrieve secrets |
| 164 | + |
| 165 | +```javascript |
| 166 | +import { DaprClient } from "@roadwork/dapr-js-sdk/http"; |
| 167 | + |
| 168 | +const daprHost = "127.0.0.1"; |
| 169 | +const daprPort = "50050"; |
| 170 | + |
| 171 | +async function start() { |
| 172 | + const client = new DaprClient(daprHost, daprPort); |
| 173 | + |
| 174 | + //Retrieve a single secret from secret store |
| 175 | + const response = await client.secret.get(SECRET_STORE_NAME, secretKey); |
| 176 | + |
| 177 | + // Retrieve all secrets from secret store |
| 178 | + const response = await client.secret.getBulk(SECRET_STORE_NAME); |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | +- For a full guide on secrets visit [How-To: Retrieve secrets]({{< ref howto-secrets.md >}}). |
| 183 | + |
| 184 | +### Actors |
| 185 | +An actor is an isolated, independent unit of compute and state with single-threaded execution. Dapr provides an actor implementation based on the [Virtual Actor pattern](https://www.microsoft.com/en-us/research/project/orleans-virtual-actors/), which provides a single-threaded programming model and where actors are garbage collected when not in use. With Dapr's implementaiton, you write your Dapr actors according to the Actor model, and Dapr leverages the scalability and reliability that the underlying platform provides. |
| 186 | + |
| 187 | +```javascript |
| 188 | +``` |
| 189 | + |
| 190 | +- For a full guide on actors visit [How-To: Use virtual actors in Dapr]({{< ref howto-actors.md >}}). |
| 191 | + |
| 192 | +## Related links |
| 193 | +- [JavaScript SDK examples](https://github.com/dapr/js-sdk/tree/master/examples) |
0 commit comments