Azure function event hub trigger to flutter web app

85 views Asked by At

I created event hub and did message routing from my iothub. Now using azure function Event hub trigger is happening and able to monitor the data in monitor section. I want this data to send to flutter for that I need an API endpoint to call from webapp so that the data can be transmitted to, but I got URL only for http trigger. Not for event hub trigger do I need to invoke URL or I need to use http trigger? What would be the best option? If any alternate option please suggest.

import azure.functions as func
import logging
import json

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)

#@app.route(route="http_trigger")
#def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
#    logging.info('Python HTTP trigger function processed a request.')

#    name = req.params.get('name')
#    if not name:
#        try:
#            req_body = req.get_json()
#        except ValueError:
#            pass
#        else:
#            name = req_body.get('name')
#    if name:
#        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
#    else:
#        return func.HttpResponse(
#             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
#             status_code=200
#        )


@app.event_hub_message_trigger(arg_name="azeventhub", event_hub_name="eventhubiot",
                               connection="AZHUB_events_IOTHUB") 
def eventhub_trigger(azeventhub: func.EventHubEvent)-> func.HttpResponse:
    logging.info('Python EventHub trigger processed an event: %s',
                azeventhub.get_body().decode('utf-8'))
    device_id = None
    temperature = None

    for azeventhub in azeventhub.get_body().decode('utf-8').split('\n'):
        if azeventhub.strip():
            key, value = azeventhub.split(':')
            if key.strip() == 'device_id':
                device_id = value.strip()
            elif key.strip() == 'temperature':
                temperature = value.strip()

    response_body = json.dumps(response_body)

    return func.HttpResponse(response_body, status_code=200, mimetype='application/json')

This is the code I'm using, above commented is previously created http trigger. I used builtin endpoint here to read data It is working fine.

1

There are 1 answers

3
Sampath On
  • The Event Hub trigger activates only when the event is updated. To get the URL, either convert the Event Hub trigger to an HTTP trigger or manually run a non-HTTP-triggered Azure Function.

The below Http Azure Function code that consumes events from an Azure Event Hub and then processes them.

   print("Device ID:", device_id)
    print("Temperature:", temperature)
    
    # Create a JSON response
    response = {
        "deviceId": device_id,
        "temperature": temperature
    }
    
    return response

def main(req: func.HttpRequest) -> func.HttpResponse:
    # Define your Azure Event Hub connection string and other details
    event_hub_conn_str = "<your_event_hub_connection_string>"
    event_hub_name = "<your_event_hub_name>"



Output: enter image description here

  • Refer for Manually run a non-HTTP-triggered Azure Functions and SO