node.jsazureazure-functionsazure-queuesazure-node-sdk

Azure : message is not getting pushed in Topics and retrieved as subscription


I have created a topic and subscription in azure. when i try to push my message in topic and retrieve it with subscription i cannot get the messages. Are messages stored in the queue. Are my messages not getting published.

Push in the topic code

 const topicName = 'xxxxxxxxxxxxx';
    async function main(){
      const sbClient = ServiceBusClient.createFromConnectionString(connectionString); 
      const topicClient = sbClient.createTopicClient(topicName);
      const sender = topicClient.createSender();

        try {

              const message= {
                body: req.body.message,
                label: `test`,

              };
              console.log(`Sending message: ${message.body}`);
              await sender.send(message);

              await topicClient.close();
              res.send(message.body)

          } finally {
            await sbClient.close();
          }

    }

    main()
    .catch((err) => {
      console.log("Error occurred: ", err);
    });

Getting Message via subscription code

const topicName = 'xxxxxxxxx';
  const subscriptionName = "subsTest1"; 

  async function main(){

    const sbClient = ServiceBusClient.createFromConnectionString(connectionString); 
    const subscriptionClient = sbClient.createSubscriptionClient(topicName, subscriptionName);
    const receiver = subscriptionClient.createReceiver(ReceiveMode.receiveAndDelete);

    try {

      const messages = await receiver.receiveMessages(10);

      res.send(messages)
      console.log("Received messages:");
      console.log(messages.map(message => message.body));

      await subscriptionClient.close();
    } finally {
      await sbClient.close();
    }
  }

  main().catch((err) => {
    console.log("Error occurred: ", err);
  });

Solution

  • I test your code, in my test I delete the request and response part, I could send and receive message. Cause in your test you don't know whether you succeed send the message, you could use ServiceBusExplorer to view the message. And remember when receive message from subscription it's slow.

    And below is my test result. Check my log, you could find the interval it won't receive the message immediately.

    enter image description here

    enter image description here