Server

Introduction

The Dapr Server will allow you to receive communication from the Dapr Sidecar and get access to its server facing features such as: Subscribing to Events, Receiving Input Bindings, and much more.

Pre-requisites

Installing and importing Dapr’s JS SDK

  1. Install the SDK with npm:
  1. npm i @dapr/dapr --save
  1. Import the libraries:
  1. import { DaprServer, CommunicationProtocolEnum } from "@dapr/dapr";
  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 Example
  7. const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
  8. // GRPC Example
  9. const server = new DaprServer(serverHost, serverPort, 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 { DaprServer } from "@dapr/dapr";
  2. const server = new DaprServer(appHost, appPort, daprHost, daprPort);
  3. // initialize subscribtions, ... before server start
  4. // the dapr sidecar relies on these
  5. await server.start();
  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

ℹ️ Note: The app-port is required here, as this is where our server will need to bind to. Dapr will check for the application to bind to this port, before finishing start-up.

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 { DaprServer, CommunicationProtocol } from "@dapr/dapr";
  2. const server = new DaprServer(appHost, appPort, daprHost, daprPort, CommunicationProtocol.GRPC);
  3. // initialize subscribtions, ... before server start
  4. // the dapr sidecar relies on these
  5. await server.start();
  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

ℹ️ Note: The app-port is required here, as this is where our server will need to bind to. Dapr will check for the application to bind to this port, before finishing start-up.

Building blocks

The JavaScript Server SDK allows you to interface with all of the Dapr building blocks focusing on Sidecar to App features.

Invocation API

Listen to an Invocation

  1. import { DaprServer, DaprInvokerCallbackContent } from "@dapr/dapr";
  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 callbackFunction = (data: DaprInvokerCallbackContent) => {
  9. console.log("Received body: ", data.body);
  10. console.log("Received metadata: ", data.metadata);
  11. console.log("Received query: ", data.query);
  12. console.log("Received headers: ", data.headers); // only available in HTTP
  13. };
  14. await server.invoker.listen("hello-world", callbackFunction, { method: HttpMethod.GET });
  15. // You can now invoke the service with your app id and method "hello-world"
  16. await server.start();
  17. }
  18. start().catch((e) => {
  19. console.error(e);
  20. process.exit(1);
  21. });

For a full guide on service invocation visit How-To: Invoke a service.

PubSub API

Subscribe to messages

Subscribing to messages can be done in several ways to offer flexibility of receiving messages on your topics:

  • Direct subscription through the subscribe method
  • Direct susbcription with options through the subscribeWithOptions method
  • Subscription afterwards through the susbcribeOnEvent method

Each time an event arrives, we pass its body as data and the headers as headers, which can contain properties of the event publisher (e.g., a device ID from IoT Hub)

Dapr requires subscriptions to be set up on startup, but in the JS SDK we allow event handlers to be added afterwards as well, providing you the flexibility of programming.

An example is provided below

  1. import { DaprServer } from "@dapr/dapr";
  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. // Method 1: Direct subscription through the `subscribe` method
  12. await server.pubsub.subscribe(pubSubName, topic, async (data: any, headers: object) =>
  13. console.log(`Received Data: ${JSON.stringify(data)} with headers: ${JSON.stringify(headers)}`),
  14. );
  15. // Method 2: Direct susbcription with options through the `subscribeWithOptions` method
  16. await server.pubsub.subscribeWithOptions(pubSubName, topic, {
  17. callback: async (data: any, headers: object) =>
  18. console.log(`Received Data: ${JSON.stringify(data)} with headers: ${JSON.stringify(headers)}`),
  19. });
  20. // Method 3: Subscription afterwards through the `susbcribeOnEvent` method
  21. // Note: we use default, since if no route was passed (empty options) we utilize "default" as the route name
  22. await server.pubsub.subscribeWithOptions("pubsub-redis", "topic-options-1", {});
  23. server.pubsub.subscribeToRoute("pubsub-redis", "topic-options-1", "default", async (data: any, headers: object) => {
  24. console.log(`Received Data: ${JSON.stringify(data)} with headers: ${JSON.stringify(headers)}`);
  25. });
  26. // Start the server
  27. await server.start();
  28. }

For a full list of state operations visit How-To: Publish & subscribe.

Subscribe to messages rule based

Dapr supports routing messages to different handlers (routes) based on rules.

E.g., you are writing an application that needs to handle messages depending on their “type” with Dapr, you can send them to different routes handlerType1 and handlerType2 with the default route being handlerDefault

  1. import { DaprServer } from "@dapr/dapr";
  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 with rule set
  11. // Note: the default route and match patterns are optional
  12. await server.pubsub.subscribe("pubsub-redis", "topic-1", {
  13. default: "/default",
  14. rules: [
  15. {
  16. match: `event.type == "my-type-1"`,
  17. path: "/type-1",
  18. },
  19. {
  20. match: `event.type == "my-type-2"`,
  21. path: "/type-2",
  22. },
  23. ],
  24. });
  25. // Add handlers for each route
  26. server.pubsub.subscribeToRoute("pubsub-redis", "topic-1", "default", async (data) => {
  27. console.log(`Handling Default`);
  28. });
  29. server.pubsub.subscribeToRoute("pubsub-redis", "topic-1", "type-1", async (data) => {
  30. console.log(`Handling Type 1`);
  31. });
  32. server.pubsub.subscribeToRoute("pubsub-redis", "topic-1", "type-2", async (data) => {
  33. console.log(`Handling Type 2`);
  34. });
  35. // Start the server
  36. await server.start();
  37. }

Dead Letter Topics

Dapr supports dead letter topic. This means that when a message fails to be processed, it gets sent to a dead letter queue. E.g., when a message fails to be handled on /my-queue it will be sent to /my-queue-failed. E.g., when a message fails to be handled on /my-queue it will be sent to /my-queue-failed.

You can use the following options with subscribeWithOptions method:

  • deadletterTopic: Specify a deadletter topic name (note: if none is provided we create one named deadletter)
  • deadletterCallback: The method to trigger as handler for our deadletter

Implementing Deadletter support in the JS SDK can be done by either

  • Passing the deadletterCallback as an option
  • By subscribing to route manually with subscribeToRoute

An example is provided below

  1. import { DaprServer } from "@dapr/dapr";
  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. // Method 1 (direct subscribing through subscribeWithOptions)
  10. await server.pubsub.subscribeWithOptions("pubsub-redis", "topic-options-5", {
  11. callback: async (data: any) => {
  12. throw new Error("Triggering Deadletter");
  13. },
  14. deadLetterCallback: async (data: any) => {
  15. console.log("Handling Deadletter message");
  16. },
  17. });
  18. // Method 2 (subscribe afterwards)
  19. await server.pubsub.subscribeWithOptions("pubsub-redis", "topic-options-1", {
  20. deadletterTopic: "my-deadletter-topic",
  21. });
  22. server.pubsub.subscribeToRoute("pubsub-redis", "topic-options-1", "default", async () => {
  23. throw new Error("Triggering Deadletter");
  24. });
  25. server.pubsub.subscribeToRoute("pubsub-redis", "topic-options-1", "my-deadletter-topic", async () => {
  26. console.log("Handling Deadletter message");
  27. });
  28. // Start server
  29. await server.start();
  30. }

Bindings API

Receive an Input Binding

  1. import { DaprServer } from "@dapr/dapr";
  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) =>
  10. console.log(`Got Data: ${JSON.stringify(data)}`),
  11. );
  12. await server.start();
  13. }
  14. start().catch((e) => {
  15. console.error(e);
  16. process.exit(1);
  17. });

For a full guide on output bindings visit How-To: Use bindings.

Configuration API

💡 The configuration API is currently only available through gRPC

Getting a configuration value

  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 client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
  8. const config = await client.configuration.get("config-redis", ["myconfigkey1", "myconfigkey2"]);
  9. }
  10. start().catch((e) => {
  11. console.error(e);
  12. process.exit(1);
  13. });

Subscribing to Key Changes

  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 client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
  8. const stream = await client.configuration.subscribeWithKeys("config-redis", ["myconfigkey1", "myconfigkey2"], () => {
  9. // Received a key update
  10. });
  11. // When you are ready to stop listening, call the following
  12. await stream.close();
  13. }
  14. start().catch((e) => {
  15. console.error(e);
  16. process.exit(1);
  17. });