> 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/realtime-communication/send-and-receive/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 and receive **1:1 text messages** over WebSocket. Assumes you completed [First connection](https://dnotifier.gitbook.io/product-docs/getting-started/first-connection/javascript-typescript.md).

***

## Send and receive in one client

```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: () => console.log("Connected as", MY_USER_ID),
  onMessage: (msg) => {
    const body = msg.payload.toJSON();
    if (body?.type === "text") {
      console.log(`[${msg.metadata.sender}] ${body.text}`);
    }
  },
  onDisconnected: () => console.log("Disconnected"),
});

await notifier.connect();

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

***

## Browser variant

Omit `WebSocketImpl` — the browser provides native WebSocket:

```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: () => {},
  onMessage: (msg) => {
    const body = msg.payload.toJSON();
    console.log(body?.text);
  },
  onDisconnected: () => {},
});

await notifier.connect();

await notifier.send({
  senderId: "user-alice",
  receiverId: "user-bob",
  data: { type: "text", text: "Hello!" },
});
```

***

## Routing by payload type

```js
onMessage: (msg) => {
  const { sender, timestamp } = msg.metadata;
  const body = msg.payload.toJSON();

  switch (body?.type) {
    case "text":
      appendChatBubble(sender, body.text, timestamp);
      break;
    case "typing":
      showTypingIndicator(sender);
      break;
    default:
      console.log("Unknown type", body);
  }
},
```

***

## Send options

```js
await notifier.send({
  senderId: MY_USER_ID,
  receiverId: PEER_USER_ID,
  data: { type: "text", text: "Persisted message" },
  saveHistory: true, // default — set false to skip chat history
});
```

***

## TypeScript

```ts
type TextPayload = { type: "text"; text: string };

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

***

## Troubleshooting

| Issue                       | Check                                                                |
| --------------------------- | -------------------------------------------------------------------- |
| `Not connected` error       | Await `connect()` first; verify `isConnected`                        |
| Receiver never gets message | Receiver must connect with same `appId`; check `receiverId` spelling |
| Empty payload               | Ensure `data` is JSON-serializable                                   |
| Auth works but send fails   | Use `transport: "ws"`, not `"http"`                                  |

***

## Learn more

* [Structured payloads](https://dnotifier.gitbook.io/product-docs/realtime-communication/structured-payloads/javascript-typescript.md)
* [Building 1:1 chat](https://dnotifier.gitbook.io/product-docs/chat/building-one-to-one-chat/javascript-typescript.md)
* [Realtime methods reference](https://dnotifier.gitbook.io/product-docs/reference/realtime-methods/javascript-typescript.md)
