> 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/one-to-one-chat/python.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# Python

```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(
            notifier.fetch_chat_history(sender_id=USER_ID, receiver_ids=[PEER_ID])
        ),
        on_message=on_message,
    )

    await notifier.connect()
    print(f"Chat ready as {USER_ID}")

    loop = asyncio.get_event_loop()
    while True:
        line = await loop.run_in_executor(None, input, "> ")
        if line.strip() == "/quit":
            break
        if not line.strip():
            continue

        await notifier.send(
            sender_id=USER_ID,
            receiver_id=PEER_ID,
            data={"type": "text", "text": line},
        )

    await notifier.disconnect()

def on_message(msg):
    body = msg.payload.to_json()
    if isinstance(body, dict) and body.get("type") == "text":
        print(f"\n[{body['text']}]\n> ", end="", flush=True)

asyncio.run(main())
```

## Run

```bash
export DNOTIFIER_APP_ID=...
export DNOTIFIER_SECRET=...
export DNOTIFIER_USER_ID=user-alice
export DNOTIFIER_PEER_ID=user-bob
python chat.py
```

Start a second instance with swapped user/peer ids for a live two-user session.

## Web framework integration

* Hold `DNotifier` in an application lifespan or dependency
* Append incoming `on_message` payloads to a message list
* Call `send()` from a form submit handler
* Load history in `on_connected` via `fetch_chat_history`
