> 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/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

## Chat client

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

const USER_ID = process.env.DNOTIFIER_USER_ID ?? "user-alice";
const PEER_ID = process.env.DNOTIFIER_PEER_ID ?? "user-bob";

const notifier = new DNotifier({
  appId: process.env.DNOTIFIER_APP_ID,
  secret: process.env.DNOTIFIER_SECRET,
  transport: "ws",
  userId: USER_ID,
  WebSocketImpl: WebSocket,
  onConnected: async () => {
    console.log(`Chat ready as ${USER_ID}`);
    await notifier.fetchChatHistory({
      senderId: USER_ID,
      receiverIds: [PEER_ID],
    });
  },
  onMessage: (msg) => {
    const body = msg.payload.toJSON();
    if (body?.type === "text") {
      console.log(`\n[${body.text}]\n> `);
    } else {
      console.log("\n[history or other payload]", body, "\n> ");
    }
  },
  onDisconnected: () => process.exit(1),
});

await notifier.connect();

const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const prompt = () => rl.question("> ", async (line) => {
  if (!line.trim()) return prompt();
  if (line === "/quit") {
    rl.close();
    return;
  }
  await notifier.send({
    senderId: USER_ID,
    receiverId: PEER_ID,
    data: { type: "text", text: line },
    saveHistory: true,
  });
  prompt();
});
prompt();
```

## Run two sessions

Terminal 1:

```bash
export DNOTIFIER_USER_ID=user-alice DNOTIFIER_PEER_ID=user-bob
node chat.js
```

Terminal 2:

```bash
export DNOTIFIER_USER_ID=user-bob DNOTIFIER_PEER_ID=user-alice
node chat.js
```

## Delete a message

```js
await notifier.deleteChatHistoryMessage({
  senderId: USER_ID,
  messageId: "message-uuid",
});
```

## Tips

* History over WebSocket arrives on `onMessage` after `fetchChatHistory`
* Over HTTP transport, `fetchChatHistory` returns the payload directly in the method result
