Python Microsoft Fabric - Receiving events as they happen

Receive events from a Fabric eventstream through a Kafka channel and act on them in a service.

Fabric can raise an event when something in its data changes, e.g. a value falls below a threshold, and a system outside Fabric often has to know about it right away. This page receives such events through a Kafka channel, and each event runs a service that passes it on.

How events reach a service

An eventstream sends events out through a destination, and a custom endpoint destination is one that speaks Kafka. A Kafka channel in Zato subscribes to it, and each event that arrives runs the channel's service. The destination can filter what it carries, so the channel receives only the events its service is for.

The channel

Under Connections → Message queues → Kafka → Channels, create the channel with the values from the Keys tab. The security definition is the same one the sending events page created, since the same app registration both sends and receives:

FieldValue
NameFabric Alerts
AddressThe bootstrap server, e.g. <namespace>.servicebus.windows.net:9093
TopicThe topic name from the Keys tab
Consumer groupzato, the group your admin created on the destination
Servicestock.notify-purchasing
SSLOn
SecurityFabric Events Token
SASL mechanismOAUTHBEARER

In enmasse YAML:

channel_kafka:
  - name: Fabric Alerts
    address: <namespace>.servicebus.windows.net:9093
    topic: <topic name>
    group_id: zato
    service: stock.notify-purchasing
    security: Fabric Events Token
    sasl_mechanism: OAUTHBEARER
    ssl: true

The service

The event arrives as the raw message, which is the JSON the eventstream sent. The service reads its fields and logs them, in the place where it would pass them on to the system that needs them.

# -*- coding: utf-8 -*-

# stdlib
import json

# Zato
from zato.server.service import Service

class NotifyPurchasing(Service):

    name = 'stock.notify-purchasing'

    def handle(self):

        # The event, as the eventstream sent it ..
        event = json.loads(self.request.raw_request)

        # .. its fields ..
        item_id = event['item_id']
        location = event['location']
        quantity = event['quantity']
        reorder_level = event['reorder_level']

        # .. and the log line the purchasing system reads.
        message = f'Stock below reorder level -> {item_id} at {location}, {quantity} left, reorder at {reorder_level}'
        self.logger.info(message)

What the server shows

When Fabric raises the event, the server log has the line within the same second:

See also

PageWhat it covers
Sending events as they happenThe other direction, and where the security definition comes from
Reading recent eventsWhen a query every minute is enough
EventsEvery field of a Kafka channel for an eventstream

Learn more