> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/examples/hello-realtime/python.md).

# Python

## Complete example

Save as `hello_realtime.py`:

```python
import asyncio
import os
from dnotifier import DNotifier

USER_ID = os.environ.get("DNOTIFIER_USER_ID", "user-alice")
PEER_ID = os.environ.get("DNOTIFIER_PEER_ID", "user-bob")

async def main():
    notifier = DNotifier(
        app_id=os.environ["DNOTIFIER_APP_ID"],
        secret=os.environ["DNOTIFIER_SECRET"],
        transport="ws",
        user_id=USER_ID,
        on_connected=lambda: asyncio.create_task(_on_connected(notifier)),
        on_message=lambda msg: print(f"[{USER_ID}] From {msg.metadata.sender}: {msg.payload.to_json()}"),
        on_disconnected=lambda code=None, reason=None: print(f"[{USER_ID}] Disconnected: {reason}"),
    )

    await notifier.connect()
    await asyncio.sleep(3600)

async def _on_connected(notifier):
    print(f"[{USER_ID}] Connected")
    await notifier.send(
        sender_id=USER_ID,
        receiver_id=PEER_ID,
        data={"type": "text", "text": "Hello from DNotifier!"},
    )
    print(f"[{USER_ID}] Message sent")

asyncio.run(main())
```

## Run

```bash
export DNOTIFIER_APP_ID=your-app-id
export DNOTIFIER_SECRET=your-app-secret
export DNOTIFIER_USER_ID=user-alice
export DNOTIFIER_PEER_ID=user-bob
python hello_realtime.py
```

## Expected output

```
[user-alice] Connected
[user-alice] Message sent
```

Run a second instance with `DNOTIFIER_USER_ID=user-bob` to receive the message.
