Rapid Node.js App development with Azure Service Bus

Summary

Azure Service Bus is a message broker service provided by Microsoft. Service Bus consists of two core technologies; Queues where a single consumer can read a message and Topics where multiple Subscribers can read the same message.

In this post, I’ll cover creating a Node.js application with Azure Service Bus. This will be a single Queue, with a Competing-Consumer EIP.

Service Bus Architecture

Queues

Topics

Max topic size – refers to the total size of the Topic container. All the messages in the Topic should not exceed 1GB.

Message time to live

Enable duplicate detection

Enable partitioning

Message Payload & Serialisation

The message and Payload will need to be synchronised; to read more about serialization go here:

https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messages-payloads

Node.JS Implementation

Node.js

  1. Create a new Service Bus Queue resource.
  2. Create a new node.js project.
  3. install Azure Service Bus dependencies.
npm install @azure/service-bus

4. Send a message to the Queue by creating a send.js file

const { ServiceBusClient } = require("@azure/service-bus"); 

// Define connection string and related Service Bus entity names here
const connectionString = "";
const queueName = ""; 

async function main(){
  const sbClient = ServiceBusClient.createFromConnectionString(connectionString); 
  const queueClient = sbClient.createQueueClient(queueName);
  const sender = queueClient.createSender();

  try {
    for (let i = 0; i < 10; i++) {
      const message= {
        body: `Hello world! ${i}`,
        label: `test`,
        userProperties: {
            myCustomPropertyName: `my custom property value ${i}`
       }
      };
      console.log(`Sending message: ${message.body}`);
      await sender.send(message);
    }

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

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

5. Receive messages from a Service Bus Queue.

const { ServiceBusClient, ReceiveMode } = require("@azure/service-bus"); 

// Define connection string and related Service Bus entity names here
const connectionString = "";
const queueName = ""; 

async function main(){
  const sbClient = ServiceBusClient.createFromConnectionString(connectionString); 
  const queueClient = sbClient.createQueueClient(queueName);
  const receiver = queueClient.createReceiver(ReceiveMode.receiveAndDelete);
  try {
    const messages = await receiver.receiveMessages(10)
    console.log("Received messages:");
    console.log(messages.map(message => message.body));

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

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

The parameters queueName and connectionString can be taken from the Shared access policies of the Queue itself.

To run the node.js application:

node send.js
node receive.js

Azure SDK For JavaScript

Service Bus / Client / @azure/service-bus / QueueClient class

QueueClient class

The createReceiver(ReceiveMode) function can take one of the following parameters:

  • ReceiveMode.peekLock: Once a message is received in this mode, the receiver has a lock on the message for a particular duration. If the message is not settled by this time, it lands back on Service Bus to be fetched by the next receive operation.
  • ReceiveMode.receiveAndDelete: Messages received in this mode get automatically removed from Service Bus.
function createReceiver(receiveMode: ReceiveMode)

Considerations

  1. Queue depth – How many messages in the Queue. Use Competing Consumers to manage Queue Depth.
  2. Push/Pull Broker type.
  3. AMQP (Advanced Message Queue Protocol).
  4. Eventual Consistency – the broker might be slow to process, but eventually, it will complete its workload.
  5. Poison Message (Dead Letter Queue).

Alternatives

  1. RabbitMQ
  2. MSMQ
  3. Azure Queues
  4. Azure ServiceBus
  5. Amazon MQ & SQS
  6. Apache Kafka (Event Streaming, but can be used as a message broker).
  7. IBM MQ

Hands-on Implementation

  1. .NET multi-tier application using Azure Service Bus queues.
  2. Quickstart: Use Service Bus queues in Azure with Node.js and the azure-sb package.

Leave a comment