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
- Create a new Service Bus Queue resource.
- Create a new node.js project.
- 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
- Queue depth – How many messages in the Queue. Use Competing Consumers to manage Queue Depth.
- Push/Pull Broker type.
- AMQP (Advanced Message Queue Protocol).
- Eventual Consistency – the broker might be slow to process, but eventually, it will complete its workload.
- Poison Message (Dead Letter Queue).
Alternatives
- RabbitMQ
- MSMQ
- Azure Queues
- Azure ServiceBus
- Amazon MQ & SQS
- Apache Kafka (Event Streaming, but can be used as a message broker).
- IBM MQ