> 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/chat/building-one-to-one-chat.md).

# Building 1:1 chat

A **1:1 chat thread** connects two user IDs over WebSocket: load history when opening the thread, send new messages with `send()`, and append live deliveries from `onMessage`.

***

## Thread model

```
  User A (user-alice)                         User B (user-bob)
         │                                           │
         │  fetchChatHistory([bob]) on open          │
         │  send({ receiverId: "user-bob" })         │
         ├──────────────────────────────────────────►│
         │                                           │
         │◄──────────────────────────────────────────┤
         │         onMessage on both sides            │
```

* **Thread key** — pair `(userA, userB)`; your app defines sort order for `fetchChatHistory`
* **Same `appId`** — both participants use the same DNotifier application
* **Stable `userId`** — map your auth user to a consistent DNotifier id

***

## Implementation checklist

1. **Connect on login** — `transport: "ws"`, `userId` = authenticated user
2. **Open thread** — call `fetchChatHistory({ senderId, receiverIds: [peerId] })`
3. **Render history** — handle history response in `onMessage`
4. **Send message** — `send({ senderId, receiverId, data: { type: "text", text } })`
5. **Live updates** — append `onMessage` payloads to the thread view
6. **Reconnect** — refetch history after disconnect to fill gaps

***

## Message payload convention

```json
{
  "type": "text",
  "text": "Hello!"
}
```

Extend with `image`, `audio`, `doc` for rich chat — see [Structured payloads](https://github.com/smartguy6666/dnotifier-sdk/blob/main/docs/chat/realtime-communication/structured-payloads/README.md).

***

## Language guides

| Language                | Guide                                                                                            |
| ----------------------- | ------------------------------------------------------------------------------------------------ |
| JavaScript / TypeScript | [javascript-typescript.md](/product-docs/chat/building-one-to-one-chat/javascript-typescript.md) |
| Dart / Flutter          | [dart-flutter.md](/product-docs/chat/building-one-to-one-chat/dart-flutter.md)                   |

***

## Related topics

| Topic        | Link                                                                    |
| ------------ | ----------------------------------------------------------------------- |
| Chat history | [Chat history](/product-docs/chat/chat-history.md)                      |
| Save history | [Save history flag](/product-docs/chat/save-history.md)                 |
| UI patterns  | [Chat UI patterns](/product-docs/chat/ui-patterns.md)                   |
| Full example | [Example: Minimal chat app](/product-docs/chat/example-minimal-chat.md) |
