> 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/getting-started/first-connection/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

This guide connects a DNotifier client over **WebSocket** from Node.js or the browser. You need [**credentials**](https://dnotifier.gitbook.io/product-docs/getting-started/credentials.md) and the [**JavaScript SDK installed**](https://dnotifier.gitbook.io/product-docs/getting-started/installation/javascript-typescript.md).

## Complete example (Node.js)

```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("Connected — ready to send messages");
    console.log("Plan limits:", notifier.getPlanLimits());
  },
  onMessage: (msg) => {
    console.log("Incoming from:", msg.metadata.sender);
    console.log("Payload:", msg.payload.toJSON());
  },
  onDisconnected: () => {
    console.log("Disconnected");
  },
});

async function main() {
  try {
    await notifier.connect();
    console.log("isConnected:", notifier.isConnected);
    console.log("messageSizeLimit:", notifier.messageSizeLimit);
  } catch (err) {
    console.error("Connect failed:", err.message);
    process.exit(1);
  }
}

main();
```

Save as `connect.js`, set environment variables, and run:

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

Expected output:

```
Connected — ready to send messages
Plan limits: { aiEnabled: true, messagesHardLimit: ..., ... }
isConnected: true
messageSizeLimit: 32768
```

## Complete example (browser)

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

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

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: () => console.log("Connected"),
  onMessage: (msg) => console.log(msg.payload.toJSON()),
  onDisconnected: () => console.log("Disconnected"),
});

await notifier.connect();
```

## Complete example (HTTP transport)

Use HTTP when you only need AI/RAG/workflows — no persistent socket:

```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-alice",
  onConnected: () => console.log("Authenticated via HTTP"),
  onMessage: () => {},
  onDisconnected: () => {},
});

await notifier.connect();
// Ready for sendAI(), search(), runWorkflow(), etc.
```

## TypeScript variant

```ts
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: (): void => {
    const limits = notifier.getPlanLimits();
    console.log("AI enabled:", limits?.aiEnabled);
  },
  onMessage: (msg): void => {
    const body = msg.payload.toJSON() as { type?: string; text?: string };
    if (body?.type === "text") {
      console.log(body.text);
    }
  },
  onDisconnected: (): void => {},
});

await notifier.connect();
```

## Common errors

| Error                                               | Likely cause                       |
| --------------------------------------------------- | ---------------------------------- |
| `appId, secret, transport, and userId are required` | Missing constructor field          |
| `Authentication Failed`                             | Wrong secret or invalid appId      |
| WebSocket errors in Node                            | Forgot `WebSocketImpl: WebSocket`  |
| Immediate disconnect                                | Network/firewall blocking `wss://` |

## What happens under the hood

1. SDK authenticates over HTTPS with your credentials
2. Auth returns a token and plan limits
3. WebSocket opens to the production host (WSS)
4. Client completes handshake with the token
5. Server acknowledges → `onConnected` fires

## Next step

Send a message to another user:

→ [**Your first message — JavaScript / TypeScript**](https://dnotifier.gitbook.io/product-docs/getting-started/first-message/javascript-typescript.md)
