# 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. This page receives such events through a Kafka channel, and each event runs a service that passes it to a system outside Fabric.

> **What you need from your Fabric admin**
>
> From the eventstream's custom endpoint destination, the Keys tab - the bootstrap server, the topic name and a consumer group created for Zato, e.g. `zato`. Authentication is the same as on the [sending events](https://zato.io/docs/dev/examples/cloud/fabric/sending-events.md) page, either the app registration allowed to receive from the eventstream or the connection string.

## How events reach a service {#how-events-reach-a-service}

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

## The channel {#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](https://zato.io/docs/dev/examples/cloud/fabric/sending-events.html) page created, since the same app registration both sends and receives:

| Field | Value |
| --- | --- |
| Name | `Fabric Alerts` |
| Address | The bootstrap server, e.g. `<namespace>.servicebus.windows.net:9093` |
| Topic | The topic name from the Keys tab |
| Consumer group | `zato`, the group your admin created on the destination |
| Service | `stock.notify-purchasing` |
| SSL | On |
| Security | `Fabric Events Token` |
| SASL mechanism | `OAUTHBEARER` |

In enmasse YAML:

```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-service}

The event arrives as the raw message, which is the JSON the eventstream sent. The service reads its fields and logs them. A production service would send them to another system at this point.

```python
# -*- 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 log them.
        message = f'Stock below reorder level -> {item_id} at {location}, {quantity} left, reorder at {reorder_level}'
        self.logger.info(message)
```

## What the server shows {#what-the-server-shows}

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

## See also {#see-also}

- [Sending events as they happen](https://zato.io/docs/dev/examples/cloud/fabric/sending-events.html) - The other direction, and where the security definition comes from
- [Reading recent events](https://zato.io/docs/dev/examples/cloud/fabric/reading-events.html) - Querying recent events on a schedule
- [Events](https://zato.io/docs/dev/examples/cloud/fabric/api/events.html) - Every field of a Kafka channel for an eventstream

## Learn more {#learn-more}

- [Development documentation](https://zato.io/docs/dev/) - Everything about writing services, in one place
- [Requests and responses](https://zato.io/docs/dev/request-response/) - What a service receives, what it returns and how to shape both
- [Integration examples](https://zato.io/docs/dev/examples/) - Ready-made code for the systems you are likely to connect to
- [IDE and debugging](https://zato.io/docs/dev/ide/) - Write services in the Dashboard or in your own editor
- [Data models](https://zato.io/docs/dev/model/) - Declare inputs and outputs and have them validated for you
- [In-depth API tutorial](https://zato.io/tutorials/main/01.html) - The full platform tutorial, from installation to production patterns
