Search code examples
dapr

Dapr pub/sub empty subscriber payload


My subscriber is being triggered when I publish a message but the data payload seems to be empty. I'm following the steps in the pub/sub subscription methods documentation.

This is the app endpoint code:

const app = express();
app.use(cors())

app.get('/dapr/subscribe', (_req, res) => {
  res.json([
    {
      pubsubname: "order-pub-sub",
      topic: "orders",
      route: "api/deliveries",
    }
  ]);
});

app.post('/api/deliveries', async (req: Request, res: Response) => {
  const rawBody = JSON.stringify(req.body);
  console.log(`Data received: ${rawBody}`)
  res.status(200).send({ status: "SUCCESS" });
});

Starting the app:

docker run -d -p 5672:5672 --name dtc-rabbitmq rabbitmq

dapr run --app-id delivery --app-port 3100 --app-protocol http --dapr-http-port 3501 --components-path ../components npm run dev

Publishing to a topic:

dapr publish --publish-app-id order --pubsub order-pub-sub --topic orders --data '{"orderId": "100"}'

Here is the console output where the payload is empty. The endpoint is triggered, but no payload.

== APP == Server started on port 3100
== APP == Data received: {}

My pubsub.yaml file:

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: order-pub-sub
spec:
  type: pubsub.rabbitmq
  version: v1
  metadata:
    - name: host
      value: "amqp://localhost:5672"
    - name: durable
      value: "false"
    - name: deletedWhenUnused
      value: "false"
    - name: autoAck
      value: "false"
    - name: reconnectWait
      value: "0"
    - name: concurrency
      value: parallel
scopes:
  - order
  - delivery

Solution

  • I didn't notice that I should expect a CloudEvents payload with my configuration.

    Modifying my code to use the appropriate body parser solved the issue:

    import bodyParser from 'body-parser';
    const app = express();
    app.use(bodyParser.json({ type: 'application/*+json' }));
    

    It would also be possible to use raw data, but for my case I'll keep as it is.