Client

Pre-requisites

Installing and importing Dapr’s JS SDK

Install the SDK with npm:

  1. npm i dapr-client

Import the libraries:

  1. import { DaprClient, DaprServer, HttpMethod, CommunicationProtocolEnum } from "dapr-client";
  2. const daprHost = "127.0.0.1"; // Dapr Sidecar Host
  3. const daprPort = "3500"; // Dapr Sidecar Port of this Example Server
  4. const serverHost = "127.0.0.1"; // App Host of this Example Server
  5. const serverPort = "50051"; // App Port of this Example Server
  6. // HTTP
  7. const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
  8. const client = new DaprClient(daprHost, daprPort);
  9. // GRPC
  10. const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.GRPC);
  11. const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
DaprClient Library

A library that provides methods for how an application communicates with the Dapr sidecar.

DaprServer Library

A library for how an application registers bindings / routes with Dapr. The startServer() method is used to start the server and bind the routes.

Building blocks

The JavaScript SDK allows you to interface with all of the Dapr building blocks.

Invoke a service

  1. import { DaprClient, HttpMethod, CommunicationProtocolEnum } from "dapr-client";
  2. const daprHost = "127.0.0.1";
  3. const daprPort = "3500";
  4. async function start() {
  5. const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
  6. const serviceAppId = "my-app-id";
  7. const serviceMethod = "say-hello";
  8. // POST Request
  9. const response = await client.invoker.invoke(serviceAppId , serviceMethod , HttpMethod.POST, { hello: "world" });
  10. // GET Request
  11. const response = await client.invoker.invoke(serviceAppId , serviceMethod , HttpMethod.GET);
  12. }

Save, get and delete application state

  1. import { DaprClient, CommunicationProtocolEnum } from "dapr-client";
  2. const daprHost = "127.0.0.1";
  3. const daprPort = "3500";
  4. async function start() {
  5. const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
  6. const serviceStoreName = "my-state-store-name";
  7. // Save State
  8. const response = await client.state.save(serviceStoreName, [
  9. {
  10. key: "first-key-name",
  11. value: "hello"
  12. },
  13. {
  14. key: "second-key-name",
  15. value: "world"
  16. }
  17. ]);
  18. // Get State
  19. const response = await client.state.get(serviceStoreName, "first-key-name");
  20. // Get Bulk State
  21. const response = await client.state.getBulk(serviceStoreName, ["first-key-name", "second-key-name"]);
  22. // State Transactions
  23. await client.state.transaction(serviceStoreName, [
  24. {
  25. operation: "upsert",
  26. request: {
  27. key: "first-key-name",
  28. value: "new-data"
  29. }
  30. },
  31. {
  32. operation: "delete",
  33. request: {
  34. key: "second-key-name"
  35. }
  36. }
  37. ]);
  38. // Delete State
  39. const response = await client.state.delete(serviceStoreName, "first-key-name");
  40. }

Publish & subscribe to messages

Publish messages
  1. import { DaprClient, CommunicationProtocolEnum } from "dapr-client";
  2. const daprHost = "127.0.0.1";
  3. const daprPort = "3500";
  4. async function start() {
  5. const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
  6. const pubSubName = "my-pubsub-name";
  7. const topic = "topic-a";
  8. const message = { hello: "world" }
  9. // Publish Message to Topic
  10. const response = await client.pubsub.publish(pubSubName, topic, message);
  11. }
Subscribe to messages
  1. import { DaprServer, CommunicationProtocolEnum } from "dapr-client";
  2. const daprHost = "127.0.0.1"; // Dapr Sidecar Host
  3. const daprPort = "3500"; // Dapr Sidecar Port of this Example Server
  4. const serverHost = "127.0.0.1"; // App Host of this Example Server
  5. const serverPort = "50051"; // App Port of this Example Server "
  6. async function start() {
  7. const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.GRPC);
  8. const pubSubName = "my-pubsub-name";
  9. const topic = "topic-a";
  10. // Configure Subscriber for a Topic
  11. await server.pubsub.subscribe(pubSubName, topic, async (data: any) => console.log(`Got Data: ${JSON.stringify(data)}`));
  12. await server.startServer();
  13. }

Interact with bindings

Output Bindings

  1. import { DaprClient, CommunicationProtocolEnum } from "dapr-client";
  2. const daprHost = "127.0.0.1";
  3. const daprPort = "3500";
  4. async function start() {
  5. const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
  6. const bindingName = "my-binding-name";
  7. const bindingOperation = "create";
  8. const message = { hello: "world" };
  9. const response = await client.binding.send(bindingName, bindingOperation, message);
  10. }

Input Bindings

  1. import { DaprServer, CommunicationProtocolEnum } from "dapr-client";;
  2. const daprHost = "127.0.0.1";
  3. const daprPort = "3500";
  4. const serverHost = "127.0.0.1";
  5. const serverPort = "5051";
  6. async function start() {
  7. const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.GRPC);;
  8. const bindingName = "my-binding-name";
  9. const response = await server.binding.receive(bindingName, async (data: any) => console.log(`Got Data: ${JSON.stringify(data)}`));
  10. await server.startServer();
  11. }

Retrieve secrets

  1. import { DaprClient, CommunicationProtocolEnum } from "dapr-client";
  2. const daprHost = "127.0.0.1";
  3. const daprPort = "3500";
  4. async function start() {
  5. const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
  6. const secretStoreName = "my-secret-store";
  7. const secretKey = "secret-key";
  8. // Retrieve a single secret from secret store
  9. const response = await client.secret.get(secretStoreName, secretKey);
  10. // Retrieve all secrets from secret store
  11. const response = await client.secret.getBulk(secretStoreName);
  12. }