2
votes

Background

I have several clients sending messages to an azure service bus queue. To match it, I need several machines reading from that queue and consuming the messages as they arrive, using Node.js.

Research

I have read the azure service bus queues tutorial and I am aware I can use receiveQueueMessage to read a message from the queue.

However, the tutorial does not mention how one can listen to a queue and read messages as soon as they arrive.

I know I can simply poll the queue for messages, but this spams the servers with requests for no real benefit.

After searching in SO, I found a discussion where someone had a similar issue:

And I know they ended up using the C# async method ReceiveAsync, but it is not clear to me if:

  1. That method is available for Node.js
  2. If that method reads messages from the queue as soon as they arrive, like I need.

Problem

The documentation for Node.js is close to non-existant, with that one tutorial being the only major document I found.

Question

  1. How can my workers be notified of an incoming message in azure bus service queues ?
3
I'm not really using Azure but had the same issue with AWS SQS. I believe only way to go is to poll for messages, since it's long polling you can open connection and wait quite a bit of time until connection is terminated. Maybe code of this npm package for SQS will help you implement it in Node.js - github.com/bbc/sqs-consumersdooo

3 Answers

2
votes

Answer

According to Azure support, it is not possible to be notified when a queue receives a message. This is valid for every language.

Work arounds

There are 2 main work arounds for this issue:

  1. Use Azure topics and subscriptions. This way you can have all clients subscribed to an event new-message and have them check the queue once they receive the notification. This has several problems though: first you have to pay yet another Azure service and second you can have multiple clients trying to read the same message.

  2. Continuous Polling. Have the clients check the queue every X seconds. This solution is horrible, as you end up paying the network traffic you generate and you spam the service with useless requests. To help minimize this there is a concept called long polling which is so poorly documented it might as well not exist. I did find this NPM module though: https://www.npmjs.com/package/azure-awesome-queue

Alternatives

Honestly, at this point, you may be wondering why you should be using this service. I agree...

As an alternative there is RabbitMQ which is free, has a community, good documentation and a ton more features.

The downside here is that maintaining a RabbitMQ fault tolerant cluster is not exactly trivial.

Another alternative is Apache Kafka which is also very reliable.

0
votes

I asked myslef the same question, here is what I found.

Use Google PubSub, it does exactly what you are looking for.

If you want to stay with Azure, the following ist possible:

  • cloud functions can be triggered from SBS messages
  • trigger an event-hub event with that cloud function
  • receive the event and fetch the message from SBS
0
votes

You can make use of serverless functions which are "ServiceBusQueueTrigger", they are invoked as soon as message arrives in queue,

Its pretty straight forward doing in nodejs, you need bindings defined in function.json which have type as

"type": "serviceBusTrigger",

This article (https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus#trigger---javascript-example) probably would help in more detail.