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

Send your first **1:1 realtime text message** after connecting over WebSocket. This example assumes you completed [**First connection**](https://dnotifier.gitbook.io/product-docs/getting-started/first-connection/javascript-typescript.md).

## Complete example

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

const MY_USER_ID = "user-alice";
const PEER_USER_ID = "user-bob";

const notifier = new DNotifier({
  appId: process.env.DNOTIFIER_APP_ID,
  secret: process.env.DNOTIFIER_SECRET,
  transport: "ws",
  userId: MY_USER_ID,
  WebSocketImpl: WebSocket,
  onConnected: async () => {
    console.log("Connected as", MY_USER_ID);

    await notifier.send({
      senderId: MY_USER_ID,
      receiverId: PEER_USER_ID,
      data: {
        type: "text",
        text: "Hello from DNotifier!",
      },
    });

    console.log("Message sent to", PEER_USER_ID);
  },
  onMessage: (msg) => {
    const body = msg.payload.toJSON();
    console.log(`[${msg.metadata.sender}]`, body);
  },
  onDisconnected: () => {
    console.log("Disconnected");
  },
});

await notifier.connect();
```

## Browser variant

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

const MY_USER_ID = "user-alice";
const PEER_USER_ID = "user-bob";

const notifier = new DNotifier({
  appId: import.meta.env.VITE_DNOTIFIER_APP_ID,
  secret: import.meta.env.VITE_DNOTIFIER_SECRET,
  transport: "ws",
  userId: MY_USER_ID,
  onConnected: () => console.log("Connected"),
  onMessage: (msg) => console.log(msg.payload.toJSON()),
  onDisconnected: () => {},
});

await notifier.connect();

await notifier.send({
  senderId: MY_USER_ID,
  receiverId: PEER_USER_ID,
  data: { type: "text", text: "Hello from DNotifier!" },
});
```

## Send after connect (decoupled)

If you prefer not to send inside `onConnected`:

```js
await notifier.connect();

await notifier.send({
  senderId: "user-alice",
  receiverId: "user-bob",
  data: {
    type: "text",
    text: "Hello from DNotifier!",
  },
  saveHistory: true, // default — set false to skip chat history persistence
});
```

## Echo test (single process)

Run two scripts in separate terminals to verify delivery:

**Terminal 1 — bob (receiver):**

```js
// receive.js — user-bob listens
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-bob",
  WebSocketImpl: WebSocket,
  onConnected: () => console.log("Bob is online"),
  onMessage: (msg) => console.log("Bob received:", msg.payload.toJSON()),
  onDisconnected: () => {},
});

await notifier.connect();
```

**Terminal 2 — alice (sender):** use the complete example at the top of this page.

Bob's terminal should print:

```
Bob received: { type: 'text', text: 'Hello from DNotifier!' }
```

## TypeScript

```ts
await notifier.send({
  senderId: MY_USER_ID,
  receiverId: PEER_USER_ID,
  data: {
    type: "text",
    text: "Hello from DNotifier!",
  } satisfies { type: string; text: string },
});
```

## Troubleshooting

| Issue                      | Check                                                                 |
| -------------------------- | --------------------------------------------------------------------- |
| Send throws before connect | Await `connect()` first; confirm `isConnected`                        |
| Bob never receives         | Bob must be connected with same `appId`; verify `receiverId` spelling |
| Empty payload              | Ensure `data` is JSON-serializable                                    |
| Auth works but send fails  | Confirm WebSocket transport, not HTTP                                 |

## Learn more

* [**Understanding messages**](https://dnotifier.gitbook.io/product-docs/getting-started/understanding-messages.md)
* [**Send & receive text — JavaScript / TypeScript**](https://dnotifier.gitbook.io/product-docs/realtime-communication/send-and-receive/javascript-typescript.md)
