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);

Running

To run the examples, you can use two different protocols to interact with the Dapr sidecar: HTTP (default) or gRPC.

Using HTTP (default)

  1. import { DaprClient, DaprServer } from "dapr-client";
  2. const client = new DaprClient(daprHost, daprPort);
  3. const server= new DaprServer(appHost, appPort, daprHost, daprPort);
  1. # Using dapr run
  2. dapr run --app-id <example-sdk> --app-port 50051 --app-protocol http npm run start
  3. # or, using npm script
  4. npm run start:dapr-http

Using gRPC

Since HTTP is the default, you will have to adapt the communication protocol to use gRPC. You can do this by passing an extra argument to the client or server constructor.

  1. import { DaprClient, DaprServer, CommunicationProtocol } from "dapr-client";
  2. const client = new DaprClient(daprHost, daprPort, CommunicationProtocol.GRPC);
  3. const server= new DaprServer(appHost, appPort, daprHost, daprPort, CommunicationProtocol.GRPC);
  1. # Using dapr run
  2. dapr run --app-id <example-sdk> --app-port 50051 --app-protocol grpc npm run start
  3. # or, using npm script
  4. npm run start:dapr-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 start() 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 } 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);
  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 } 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);
  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 } 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);
  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 } 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);
  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.start();
  13. }

Interact with bindings

Output Bindings

  1. import { DaprClient } 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);
  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 } 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);;
  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.start();
  11. }

Retrieve secrets

  1. import { DaprClient } 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);
  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. }

Get configuration keys

  1. import { DaprClient } from "dapr-client";
  2. const daprHost = "127.0.0.1";
  3. const daprAppId = "example-config";
  4. async function start() {
  5. const client = new DaprClient(
  6. daprHost,
  7. process.env.DAPR_HTTP_PORT
  8. );
  9. const config = await client.configuration.get('config-store', ['key1', 'key2']);
  10. console.log(config);
  11. console.log(JSON.stringify(config));
  12. }
  13. start().catch((e) => {
  14. console.error(e);
  15. process.exit(1);
  16. });