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

# Python

Send and receive **1:1 text messages** over WebSocket. Assumes you completed [First connection](https://dnotifier.gitbook.io/product-docs/getting-started/first-connection/python.md).

***

## Send and receive in one client

```python
import asyncio
from dnotifier import DNotifier

MY_USER_ID = "user-alice"
PEER_USER_ID = "user-bob"

async def main():
    notifier = DNotifier(
        app_id="your-app-id",
        secret="your-app-secret",
        transport="ws",
        user_id=MY_USER_ID,
        on_connected=lambda: print(f"Connected as {MY_USER_ID}"),
        on_message=lambda msg: _handle_message(msg),
        on_disconnected=lambda code=None, reason=None: print(f"Disconnected: {reason}"),
    )

    await notifier.connect()

    await notifier.send(
        sender_id=MY_USER_ID,
        receiver_id=PEER_USER_ID,
        data={"type": "text", "text": "Hello from DNotifier!"},
    )

    await asyncio.sleep(3600)

def _handle_message(msg):
    body = msg.payload.to_json()
    if isinstance(body, dict) and body.get("type") == "text":
        print(f"[{msg.metadata.sender}] {body['text']}")

asyncio.run(main())
```

***

## Routing by payload type

```python
def on_message(msg):
    body = msg.payload.to_json()
    if not isinstance(body, dict):
        return

    msg_type = body.get("type")
    if msg_type == "text":
        append_chat_bubble(msg.metadata.sender, body["text"], msg.metadata.timestamp)
    elif msg_type == "typing":
        show_typing_indicator(msg.metadata.sender)
    else:
        print(f"Unknown type: {body}")
```

***

## Send options

```python
await notifier.send(
    sender_id=MY_USER_ID,
    receiver_id=PEER_USER_ID,
    data={"type": "text", "text": "Persisted message"},
    save_history=True,  # default — set False to skip chat history
)
```

***

## Troubleshooting

| Issue                                              | Check                                                       |
| -------------------------------------------------- | ----------------------------------------------------------- |
| `RuntimeError: Not connected`                      | Await `connect()` before `send()`                           |
| Receiver gets nothing                              | Same `app_id`; receiver must be connected for live delivery |
| `ValueError: receiver_id or receiver_ids required` | Pass `receiver_id` or `receiver_ids`                        |
| Auth OK but no delivery                            | Use `transport="ws"`, not `"http"`                          |

***

## Learn more

* [Structured payloads](https://dnotifier.gitbook.io/product-docs/realtime-communication/structured-payloads/python.md)
* [Building 1:1 chat](https://dnotifier.gitbook.io/product-docs/chat/building-one-to-one-chat/python.md)
* [Realtime methods reference](https://dnotifier.gitbook.io/product-docs/reference/realtime-methods/python.md)
