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

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

const notifier = new DNotifier({
  appId: process.env.DNOTIFIER_APP_ID,
  secret: process.env.DNOTIFIER_SECRET,
  transport: "http",
  userId: "user-123",
  logs: true,
  onConnected: () => {},
  onMessage: () => {},
  onDisconnected: () => {},
});

await notifier.connect();

const limits = notifier.getPlanLimits();
if (!limits?.aiEnabled) {
  throw new Error("AI is not enabled on this plan");
}

// Simple Q&A
const response = await notifier.sendAI({
  senderId: "user-123",
  message: { text: "What are three benefits of realtime messaging?" },
});

console.log("AI response:", JSON.stringify(response, null, 2));

// Chat-style follow-up in the same session
const sessionId = response?.metadata?.packet?.id ?? response?.id;

const followUp = await notifier.sendAI({
  senderId: "user-123",
  sessionId,
  message: {
    messages: [
      { role: "user", content: "What are three benefits of realtime messaging?" },
      { role: "assistant", content: response?.data?.content ?? "" },
      { role: "user", content: "Give me one concrete example for each." },
    ],
  },
  saveHistory: true,
});

console.log("Follow-up:", JSON.stringify(followUp, null, 2));

// Fetch history
const history = await notifier.fetchAIHistory({ senderId: "user-123" });
console.log("History entries:", history);
```

## Run

```bash
export DNOTIFIER_APP_ID=your-app-id
export DNOTIFIER_SECRET=your-app-secret
node ai-qa.js
```

## Session logging

With `logs: true`, session start, completion, and token usage appear in the DNotifier logs dashboard.
