javascriptazurefunctionazure-functionsservicebus

How to write an Azure function trigger for a new Azure service bus entry in JavaScript?


I've tried a lot, but I don't understand how to configure the following:

When there is a new entry in the Azure service bus, I want an Azure function (4.0) to get triggered.

This is how I expected it to work:

const { app } = require('@azure/functions');

const namespace = "this-is-my-service-bus-namespace"
const fullyQualifiedNamespace = `${namespace}.servicebus.windows.net`;

const topicName = "my-topic";

app.serviceBusTopic('serviceBusQueueTrigger1', {
    topicName: topicName,
    connection: fullyQualifiedNamespace,
    handler: (message, context) => {
        context.log('message:', message);
    },
});

This gives me an error when it starts up:

The 'serviceBusQueueTrigger1' function is in error: Unable to configure binding 'serviceBusTrigger1' of type 'serviceBusTrigger'. This may indicate invalid function.json properties. Can't figure out which ctor to call.

What does this mean? I don't even have a file called function.json.

Microsoft offers some documentation here, but I don't understand what exactly I have to do. Probably, something with the connection is wrong... Do I have to put settings to a JSON file?


Solution

  • When there is a new entry in the Azure service bus, I want an Azure function (4.0) to get triggered.

    You can create a Model v4 JavaScript Service Bus topic triggered function like below-

    Add subscriptionName: '{subscription name which is created in your topic}' in your code alike me.

    Code-

    const { app } =  require('@azure/functions');
    
    app.serviceBusTopic('serviceBusTopicTrigger1', {
    connection:  'afreensb01_SERVICEBUS',
    topicName:  '{your topic name}',
    subscriptionName:  'mysubscription',
    handler: (message, context) => {
    context.log('Service bus topic function processed message:', message);
    }
    });
    

    You need to copy the connection string as shown below and pass the value in local.settings file.

    enter image description here

    local.settings.json-

    {
    "IsEncrypted": false,
    
    "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    "afreensb01_SERVICEBUS": "Endpoint=sb://afreen-sb-01.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=************"
    }
    }
    

    Create a subscription in your topic like below.

    enter image description here

    I am sending the message to my topic by selecting the subscription.

    enter image description here

    I am able to get the expected output.

    enter image description here

    Please make the changes in your code, you will also get the output.