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

# JavaScript / TypeScript

End-to-end pattern for a two-user chat thread.

***

## Chat client skeleton

```js
import WebSocket from "ws";
import { DNotifier } from "@dnotifier-realtime/dnotifier";

const MY_USER_ID = "user-alice";
const PEER_USER_ID = "user-bob";
const messages = [];

const notifier = new DNotifier({
  appId: process.env.DNOTIFIER_APP_ID,
  secret: process.env.DNOTIFIER_SECRET,
  transport: "ws",
  userId: MY_USER_ID,
  WebSocketImpl: WebSocket,
  onConnected: async () => {
    await openThread(PEER_USER_ID);
  },
  onMessage: (msg) => {
    const body = msg.payload.toJSON();

    // History response or live message
    if (Array.isArray(body?.messages)) {
      messages.push(...body.messages);
      renderThread(messages);
      return;
    }

    if (body?.type === "text") {
      messages.push({
        sender: msg.metadata.sender,
        text: body.text,
        timestamp: msg.metadata.timestamp,
        id: msg.metadata.id,
      });
      renderThread(messages);
    }
  },
  onDisconnected: () => showOfflineState(),
});

async function openThread(peerId) {
  await notifier.fetchChatHistory({
    senderId: MY_USER_ID,
    receiverIds: [peerId],
  });
}

async function sendText(text) {
  await notifier.send({
    senderId: MY_USER_ID,
    receiverId: PEER_USER_ID,
    data: { type: "text", text },
    saveHistory: true,
  });
}

await notifier.connect();
```

***

## Send on user input

```js
document.querySelector("#send-btn").addEventListener("click", async () => {
  const input = document.querySelector("#message-input");
  const text = input.value.trim();
  if (!text || !notifier.isConnected) return;

  await sendText(text);
  input.value = "";
});
```

Optimistic UI: append the outgoing message locally before `send()` resolves, then reconcile with `metadata.id` from any server echo if needed.

***

## Typing indicator (optional)

```js
await notifier.send({
  senderId: MY_USER_ID,
  receiverId: PEER_USER_ID,
  data: { type: "typing" },
  saveHistory: false,
});
```

Handle in `onMessage`:

```js
if (body?.type === "typing") {
  showTypingIndicator(msg.metadata.sender);
}
```

***

## After reconnect

```js
onDisconnected: () => {
  showOfflineState();
  scheduleReconnect(async () => {
    await notifier.connect();
    await openThread(PEER_USER_ID);
  });
},
```

→ [Error handling & reconnection](https://github.com/smartguy6666/dnotifier-sdk/tree/main/docs/chat/realtime-communication/error-handling.md)

***

## Learn more

* [Chat history](https://github.com/smartguy6666/dnotifier-sdk/tree/main/docs/chat/building-one-to-one-chat/chat-history/javascript-typescript.md)
* [Example: Minimal chat app](https://github.com/smartguy6666/dnotifier-sdk/tree/main/docs/chat/building-one-to-one-chat/example-minimal-chat/javascript-typescript.md)
