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

## Complete example (Node.js)

Save as `hello-realtime.js`:

```js
import WebSocket from "ws";
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(`[${USER_ID}] Connected`);

    const messageId = await notifier.send({
      senderId: USER_ID,
      receiverId: PEER_ID,
      data: { type: "text", text: "Hello from DNotifier!" },
    });
    console.log("Sent message:", messageId);
  },
  onMessage: (msg) => {
    const body = msg.payload.toJSON();
    console.log(`[${USER_ID}] From ${msg.metadata.sender}:`, body);
  },
  onDisconnected: ({ reason }) => {
    console.log(`[${USER_ID}] Disconnected:`, reason);
  },
});

await notifier.connect();
```

## Run

```bash
export DNOTIFIER_APP_ID=your-app-id
export DNOTIFIER_SECRET=your-app-secret
export DNOTIFIER_USER_ID=user-alice
export DNOTIFIER_PEER_ID=user-bob
node hello-realtime.js
```

## Browser variant

Omit `WebSocketImpl` and use your bundler's env vars for credentials.

## Expected output

```
[user-alice] Connected
Sent message: <uuid>
```

When `user-bob` is running the same script with swapped ids, both sides log incoming payloads.
