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

Package: `@dnotifier-realtime/dnotifier`

## Constructor

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

const notifier = new DNotifier({
  appId: "your-app-id",
  secret: "your-app-secret",
  transport: "ws", // or "http"
  userId: "user-123",
  logs: false,
  url: undefined, // optional custom WebSocket URL
  WebSocketImpl: undefined, // required in Node.js when transport is "ws"
  onConnected: () => {},
  onMessage: (msg) => {},
  onDisconnected: ({ code, reason }) => {},
});
```

## Option reference

| Option                  | Type                                           | Required         | Default                      | Description                                          |
| ----------------------- | ---------------------------------------------- | ---------------- | ---------------------------- | ---------------------------------------------------- |
| `appId`                 | `string`                                       | ✅                | —                            | DNotifier application id                             |
| `secret`                | `string`                                       | ✅                | —                            | Application secret                                   |
| `transport`             | `"ws"` \| `"http"`                             | ✅                | —                            | Transport mode                                       |
| `userId`                | `string`                                       | ✅                | —                            | User or agent id for auth and routing                |
| `logs`                  | `boolean`                                      | —                | `false`                      | Enable AI session logging to the dashboard           |
| `url`                   | `string`                                       | —                | production WSS (SDK default) | Override WebSocket URL only if provided by DNotifier |
| `WebSocketImpl`         | `class`                                        | Node + `ws` only | `globalThis.WebSocket`       | WebSocket constructor for Node.js                    |
| `onConnected`           | `() => void`                                   | —                | `() => {}`                   | Ready callback                                       |
| `onMessage`             | `(msg: DNotifierMessage) => void`              | —                | `() => {}`                   | Incoming message callback                            |
| `onDisconnected`        | `({ code?: number, reason?: string }) => void` | —                | `() => {}`                   | Disconnect/error callback                            |
| `permissionEnforcement` | `"backend"` \| `"disabled"`                    | —                | `"backend"`                  | Tool permission checks before execute                |
| `onAgentEvent`          | `(event: object) => void`                      | —                | —                            | Local agent/tool/MCP event callback                  |

Throws `Error` if `appId`, `secret`, `transport`, or `userId` is missing.

Agent tools and MCP: see [**Tools & MCP agents**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/tools-and-mcp/javascript-typescript.md).

## Node.js WebSocket example

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

const notifier = new DNotifier({
  appId: process.env.DNOTIFIER_APP_ID,
  secret: process.env.DNOTIFIER_SECRET,
  transport: "ws",
  userId: "user-alice",
  WebSocketImpl: WebSocket,
  onConnected: () => console.log("ready"),
  onMessage: () => {},
  onDisconnected: () => {},
});
```

## Browser example

Omit `WebSocketImpl` — the browser provides `WebSocket` globally:

```js
const notifier = new DNotifier({
  appId: import.meta.env.VITE_DNOTIFIER_APP_ID,
  secret: import.meta.env.VITE_DNOTIFIER_SECRET,
  transport: "ws",
  userId: "user-alice",
  onConnected: () => {},
  onMessage: () => {},
  onDisconnected: () => {},
});
```

## HTTP transport example

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

## Instance properties (after `connect()`)

| Property           | Type      | Description                          |
| ------------------ | --------- | ------------------------------------ |
| `appId`            | `string`  | Constructor value                    |
| `secret`           | `string`  | Constructor value                    |
| `userId`           | `string`  | Constructor value                    |
| `url`              | `string`  | Resolved WebSocket URL               |
| `isConnected`      | `boolean` | Connection state                     |
| `aiEnabled`        | `boolean` | Plan AI flag                         |
| `authToken`        | `string`  | Auth token                           |
| `messageSizeLimit` | `number`  | Chunk size limit in bytes            |
| `currentModel`     | `string`  | Model name from auth (when provided) |

## TypeScript callback types

```ts
type OnMessage = (msg: {
  metadata: { id?: string; sender: string; timestamp: number; type?: string };
  payload: { toJSON(): unknown; toString(encoding?: string): string; toBase64(): string; raw(): ArrayBuffer | Buffer };
}) => void;

type OnDisconnected = (info: { code?: number; reason?: string }) => void;
```

## Notes

* The JavaScript SDK does not expose a `disconnect()` method. Close the underlying socket by letting the process exit or managing transport lifecycle in your app layer.
* Custom `url` applies to WebSocket transport only, and only when DNotifier provides a dedicated host. Auth and HTTP RPC use the production API host built into the SDK.
