@azure/functions-extensions-servicebus
v0.4.1
Published
Node.js Azure ServiceBus extension implementations for Azure Functions
Readme
Azure Functions Node.js Extensions - Service Bus
This project provides extensions for working with Azure Service Bus in Azure Functions using Node.js. It simplifies the integration and interaction with Service Bus, enabling developers to build scalable and efficient serverless applications.
Features
- Service Bus Trigger: Automatically trigger Azure Functions when messages arrive in queues or subscriptions.
- Service Bus Input Binding: Read Service Bus messages as input to your function.
- Service Bus Output Binding: Send messages to Service Bus queues and topics.
- Cached Client Management: Optimized client reuse with intelligent caching
- Resource Management: Proper disposal patterns for Azure resource clients
- Performance Optimizations: Reduced overhead for messaging operations
Prerequisites
- Node.js (LTS version recommended)
- Azure Functions Core Tools
- An active Azure subscription
Installation
To install the required dependencies, run:
npm install @azure/functions-extensions-servicebusUsage
- Set up your Azure Service Bus namespace:
- Create a Service Bus namespace in the Azure portal.
- Create queues, topics, and subscriptions as needed.
- Configure your function:
- Update the
function.jsonfile to include the appropriate bindings for Service Bus.
- Import and initialize the extension:
import '@azure/functions-extensions-servicebus'; // Ensures at the top of the fileUsing in Azure Functions
import "@azure/functions-extensions-servicebus";
import { ServiceBusMessageContext } from "@azure/functions-extensions-servicebus"
import { app, InvocationContext } from "@azure/functions";
export async function serviceBusTrigger1(
serviceBusMessageContext: ServiceBusMessageContext,
context: InvocationContext
): Promise<void> {
try {
// Log trigger metadata
context.log("triggerMetadata: ", context.triggerMetadata);
// Process the first message (messages is always an array)
const message = serviceBusMessageContext.messages[0];
context.log('Completing the message', message);
// Use serviceBusMessageActions to action on the messages
await serviceBusMessageContext.actions.complete(message);
context.log('Completing the body', message.body);
} catch (error) {
context.log('Error processing Service Bus message:', error);
}
}
app.serviceBusQueue("serviceBusTrigger", {
connection: "AzureWebJobsServiceBus",
queueName: "testqueue",
cardinality: "many",
sdkBinding: true, //Ensure this is set to true
autoCompleteMessages: false, //Exposing this so that customer can take action on the messages
handler: serviceBusTrigger1,
});
// https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus?tabs=isolated-process%2Cextensionv5&pivots=programming-language-javascript
// autoCompleteMessages
- Run locally:
- Use the Azure Functions Core Tools to run your function locally:
func start- Deploy to Azure:
- Deploy your function to Azure using the following command:
func azure functionapp publish <YourFunctionAppName>Contributing
Contributions are welcome! Please follow the contribution guidelines when submitting issues or pull requests.
License
This project is licensed under the MIT License.
