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 } 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. await server.invoker.listen('hello-world', mock, { method: HttpMethod.GET });
  9. // You can now invoke the service with your app id and method "hello-world"
  10. await server.start();
  11. }
  12. start().catch((e) => {
  13. console.error(e);
  14. process.exit(1);
  15. });

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

PubSub API

Subscribe to messages

  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. await server.pubsub.subscribe(pubSubName, topic, async (data: any) => console.log(`Got Data: ${JSON.stringify(data)}`));
  12. await server.start();
  13. }
  14. start().catch((e) => {
  15. console.error(e);
  16. process.exit(1);
  17. });

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

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

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