How to use DaprServer instance for exposing API endpoints and receiving events in Javascript at the same time?

20 Views Asked by At

Given the following JavaScript application running on Node on the Dapr runtime:

import { DaprServer, CommunicationProtocolEnum } from "@dapr/dapr";
import express from "express";

const myApp = express();

myApp.get("/my-custom-endpoint", (req, res) => {
  res.send({ msg: "My own express app!" });
});

const daprServer = new DaprServer({
      serverHost: "127.0.0.1",
      serverPort: "3000",
      serverHttp: myApp,
    });

await daprServer.pubsub.subscribe("pubsub", "messages", async (messageId) => {
        console.log(`Subscriber received: ${JSON.stringify(messageId)}`)
    });

await daprServer.start();

In this example, we would like to use the daprServer instance to both:

  • Expose HTTP API endpoints
  • Listen to events from the pubsub pubsub on the messages topic

However when running this via dapr run --app-id background-with-api --app-port 3000 -- node main.js, event listening seems to be disabled.

The same setup is actually feasible with C# and asp.net core, since node now supports threads I don't see why this doesn't work with Javascript?

Am I missing something ?

0

There are 0 best solutions below