Server

    Pre-requisites

    1. Install the SDK with :
    1. Import the libraries:
    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. // HTTP Example
    7. const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
    8. // GRPC Example

    Running

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

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

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

    Listen to an Invocation

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

    Receive an Input Binding

    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. }
    12. start().catch((e) => {
    13. console.error(e);
    14. process.exit(1);