Jonah Ho
Jonah Ho

Reputation: 45

Event Grid Namespace with MQTT Enabled: Event Subscription processes event where data key is null

We have created an Event Grid Namespace with the MQTT feature enabled.

From there we already route the data to an Event-Hubs (as in this tutorial) which triggers an Azure Function. This way works as expected.

Now we want to directly trigger an Azure Function directly from the Event Grid for specific Mqtt topics. Therefore we created a new Event Subscription inside the Event Grid Topic with a prefix filter that matches our Mqtt topic. The filtering works. We are receiving messages, using the Python Azure functions library.

Problem: Data field of event messages is null.

Expected behaviour: The data field of the event message has the value of the json payload of the mqtt message.

We are able to receive messages with the python libary when we send Mqtt Events but the 'data' field is null also the event_type is null.

Here is an example payload we receive in the Azure fuction: tested with azure-functions 1.17.0 https://pypi.org/project/azure-functions/ and azure-functions 1.18.0b3 https://pypi.org/project/azure-functions/1.18.0b3/

def main(event: func.EventGridEvent, signalRMessages: func.Out[str]) -> None:
    event_dict = {
        'id': event.id,
        'data': event.get_json(),
        'topic': event.topic,
        'subject': event.subject,
        'event_type': event.event_type,
    }

# Output: {"id": "1572f624-9581-4210-8bf4-6e121c6bd649", "data": null, "topic": null, "subject": "dockerhost02/Kathrein-RTLS/test1", "event_type": null}

Upvotes: 0

Views: 516

Answers (1)

Sampath
Sampath

Reputation: 3639

The main issue is the data format in which you are sending the message.

  • I used this MSDOC reference to publish a message from Event Grid with MQTT.

  • The code reference was taken from the Git repository.

  • This is the sample code for sending data to Event Grid.

topic = "vehicles/{client_id}/position".format(client_id=client_id)
        while True:
            lat = round(random.uniform(-90,90),6)
            lon = round(random.uniform(-180,180),6)
            # do a random selction from latitude and longitude
            p1 = Point(lat, lon)
            payload = str(p1)
            publish_result = mqtt_client.publish(topic, payload)
            print(f"Sending publish with payload \"{payload}\" on topic \"{topic}\" with message id {publish_result.mid}")
            time.sleep(10)
  • The playlod load formate should be in json.
topic = {
    "temperature": 25.5,
    "humidity": 60.0,
    "status": "OK"
}

enter image description here

  • Connect the Event Grid to the Event Function.

enter image description here

enter image description here

  • The code reference for Azure Event Grid trigger for Azure Functions is taken from MSDOC .

enter image description here

Upvotes: 0

Related Questions