> 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/structured-payloads/python.md).

# Python

Send images, audio, and documents via `send()` with typed `data` objects.

***

## Image

```python
from pathlib import Path
from dnotifier import DNotifier

async def send_image(notifier: DNotifier):
    image_bytes = Path("path/to/photo.png").read_bytes()

    await notifier.send(
        sender_id="user-alice",
        receiver_id="user-bob",
        data={"type": "image", "content": image_bytes},
    )
```

### With file metadata

```python
async def send_image_with_meta(notifier: DNotifier, file_path: str):
    image_bytes = Path(file_path).read_bytes()

    await notifier.send(
        sender_id=my_user_id,
        receiver_id=peer_user_id,
        data={
            "type": "image",
            "content": image_bytes,
            "fileName": Path(file_path).name,
            "mimeType": "image/jpeg",
        },
    )
```

***

## Audio

```python
from pathlib import Path

async def send_audio(notifier: DNotifier):
    audio_bytes = Path("path/to/voice-note.m4a").read_bytes()

    await notifier.send(
        sender_id="user-alice",
        receiver_id="user-bob",
        data={
            "type": "audio",
            "content": audio_bytes,
            "durationMs": 12500,
            "mimeType": "audio/mp4",
        },
    )
```

***

## Document

```python
from pathlib import Path

async def send_document(notifier: DNotifier):
    doc_bytes = Path("path/to/report.pdf").read_bytes()

    await notifier.send(
        sender_id="user-alice",
        receiver_id="user-bob",
        data={
            "type": "doc",
            "content": doc_bytes,
            "fileName": "report.pdf",
            "mimeType": "application/pdf",
        },
    )
```

***

## Receiving structured payloads

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

    msg_type = body.get("type")
    if msg_type == "image":
        content = body.get("content")
        if isinstance(content, (bytes, bytearray)):
            show_image_preview(content)
    elif msg_type == "audio":
        play_audio(body.get("content"), body.get("mimeType"))
    elif msg_type == "doc":
        save_and_open_file(body.get("content"), body.get("fileName"), body.get("mimeType"))
```

***

## Size limits

```python
await notifier.connect()
print(f"Max chunk size: {notifier.message_size_limit}")
```

Payloads larger than `message_size_limit` are chunked automatically.

→ [Large messages & chunking](https://dnotifier.gitbook.io/product-docs/realtime-communication/chunking.md)

***

## Learn more

* [Binary messaging](https://dnotifier.gitbook.io/product-docs/realtime-communication/binary-messaging/python.md)
* [Chat UI patterns](https://dnotifier.gitbook.io/product-docs/chat/ui-patterns.md)
