Skip to content

Subscribe is not working if daprServer.start() is invoked #717

@stperin

Description

@stperin

Expected Behavior

Subscribe made before daprServer.start()

Actual Behavior

Subscribe after before daprServer.start()

Steps to Reproduce the Problem

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { DaprServer } from '@dapr/dapr';
import * as express from 'express';
import { ExpressAdapter } from '@nestjs/platform-express';

async function bootstrap() {
  const port = '3002';
  const server = express();

  const pubsubName = 'pubsub-name';

  // I'using JetStream as queue
  const topic1 = 'xxx.>';
  const topic2 = 'yyy.>';

  server.use((req, res, next) => {
    if (req.path.startsWith('/dapr') || req.path.startsWith('/events/'))
      return next();
    return next();
  });

  const daprServer = new DaprServer({
    serverHost: 'localhost',
    serverPort: port,
    serverHttp: server,
    clientOptions: {
      daprHost: 'localhost',
      daprPort: '3502',
    },
  });

  await daprServer.pubsub.subscribe(
    pubsubName,
    topic1,
    async (data) => console.log(topic1),
    undefined,
    { rawPayload: true },
  );

  await daprServer.start();

  await daprServer.pubsub.subscribe(
    pubsubName,
    topic2,
    async (data) => console.log(topic2),
    undefined,
    { rawPayload: true },
  );

  const app = await NestFactory.create(AppModule, new ExpressAdapter(server));
  await app.init();
}
bootstrap();

Proposed fix

Fix server subscribe documentation and move subscribe before daprSever.start()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions