Node.js Azure ServiceBus extension implementations for Azure Functions
npm install @azure/functions-extensions-servicebusbash
npm install @azure/functions-extensions-servicebus
`
$3
1. Set up your Azure Service Bus namespace:
- Create a Service Bus namespace in the Azure portal.
- Create queues, topics, and subscriptions as needed.
2. Configure your function:
- Update the function.json file to include the appropriate bindings for Service Bus.
3. Import and initialize the extension:
`javascript
import '@azure/functions-extensions-servicebus'; // Ensures at the top of the file
`
Using in Azure Functions
`javascript
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 {
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
`
4. Run locally:
- Use the Azure Functions Core Tools to run your function locally:
`bash
func start
`
5. Deploy to Azure:
- Deploy your function to Azure using the following command:
`bash
func azure functionapp publish
``