Timeout in Azure functions App binding with events hub to send data

Ali, Muhammad 0 Reputation points
2023-07-02T10:53:50.9233333+00:00

I have a nodeJS functions app, in which i have a http trigger function with following code. It runs successfully.

With all the below code and configuration, i am able to send data to events hub from a local functions app and published functions app, the issue which i am facing is when i run function from portal it takes forever to return response and then it gives 500 error, before adding this events hub code it was running fine on portal.

I am following guide from Microsof site but looks like it is not how it should send the message to events hub and i tried adding await to lines sending data, i have also tried commenting context.done(), and i also tried putting it in a function and calling it with await but still all gives me same behavior.

Same behavior i have seen with timer trigger function and it gives me following exception after running. It looks like it is sending the data but then after sometime it gives a timeout.

[Error] Executed 'Functions.myfunction' (Failed, Id=, Duration=131873ms)

Below is my code before adding events hub code (runs fine on local and portal)

module.exports =  async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
try {
const name = (req.query.name || (req.body && req.body.name));
const responseMessage = name
    ? "Hello, " + name + ". This HTTP triggered function executed successfully."
    : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";


    // Construct response
    const responseJSON = {
        "message": responseMessage,
        "success": true
    }

    context.res = {
        // status: 200, /* Defaults to 200 */
        body: responseJSON,
        contentType: 'application/json'
    };
} catch(err) {
    context.res = {
        status: 500
    };
}
}

I then upgraded my code to following to send data from functions app to events hub this source

module.exports =  async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
try {
const name = (req.query.name || (req.body && req.body.name));
const responseMessage = name
    ? "Hello, " + name + ". This HTTP triggered function executed successfully."
    : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";


    var timeStamp = new Date().toISOString();
    var message = 'Message created at: ' + timeStamp;

    context.bindings.outputEventHubMessage = [];

    context.bindings.outputEventHubMessage.push("1 " + message);
    context.bindings.outputEventHubMessage.push("2 " + message);
    // context.done();

    // Construct response
    const responseJSON = {
        "message": responseMessage,
        "success": true
    }

    context.res = {
        // status: 200, /* Defaults to 200 */
        body: responseJSON,
        contentType: 'application/json'
    };
} catch(err) {
    context.res = {
        status: 500
    };
}
}

I have also added binding of events hub

My functions.json

{
      "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "eventHub",
      "direction": "out",
      "name": "outputEventHubMessage",
      "connection": "smaseventhubsend_EVENTHUBSETTING",
      "eventHubName": "smaseventhub"
    }
  ]
}

My local.settings.json file

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "smaseventhubsend_EVENTHUBSETTING": "Pathtoeventhub with key"
  }
}
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MayankBargali-MSFT 70,936 Reputation points Moderator
    2023-07-03T12:49:21.7566667+00:00

    @Ali, Muhammad Thanks for reaching out. I have reviewed and tested your code at my end, and it works as expected. Can you please confirm if your application setting has the smaseventhubsend_EVENTHUBSETTING application setting name and value as your event hub connection string as if this would have missed from configuration then you would observe the 500 error. In case if the application setting is already updated then I will suggest you to navigation to Diagnose and solve problems to know the cause of 500 error and the steps if any to mitigate the issue.

    Feel free to get back to me if you need any assistance.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.