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

Requires `transport: "ws"` for push delivery to `onMessage`. `send()` also works over HTTP but is not ideal for live chat UX.

## `send(options)`

Send a structured message to one or more receivers.

| Parameter     | Type       | Required | Default | Description                                |
| ------------- | ---------- | -------- | ------- | ------------------------------------------ |
| `senderId`    | `string`   | ✅        | —       | Sender user id (unprefixed)                |
| `receiverId`  | `string`   | \*       | —       | Single recipient                           |
| `receiverIds` | `string[]` | \*       | —       | Multiple recipients                        |
| `data`        | `object`   | ✅        | —       | Payload body (any JSON-serializable shape) |
| `saveHistory` | `boolean`  | —        | `true`  | Persist to chat history                    |

\* Provide `receiverId` or `receiverIds`.

**Server metadata:** `type: "text"`, `code: 200`

**Returns:**

| Transport | Return                    |
| --------- | ------------------------- |
| `ws`      | `messageId` (UUID string) |
| `http`    | RPC response object       |

**Chunking:** Payloads larger than `messageSizeLimit` are split into multiple frames with shared `id` and incrementing `currentPacket`.

### Example — text

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

### Example — multiple receivers

```js
await notifier.send({
  senderId: "user-alice",
  receiverIds: ["user-bob", "user-carol"],
  data: { type: "text", text: "Hello everyone" },
});
```

### Example — image bytes

```js
import { readFileSync } from "fs";

await notifier.send({
  senderId: "user-alice",
  receiverId: "user-bob",
  data: {
    type: "image",
    content: readFileSync("./photo.png"),
  },
});
```

## `sendBinary(options)`

Send raw bytes without JSON serialization.

| Parameter     | Type                     | Required | Default    | Description         |
| ------------- | ------------------------ | -------- | ---------- | ------------------- |
| `senderId`    | `string`                 | ✅        | —          | Sender user id      |
| `receiverIds` | `string[]`               | ✅        | —          | Recipients          |
| `buffer`      | `Uint8Array` \| `Buffer` | ✅        | —          | Raw bytes           |
| `type`        | `string`                 | —        | `"binary"` | Metadata type field |

**Server metadata:** `code: 202`

**Returns:** `void`

**Transport:** WebSocket only (calls `transport.sendBinary` directly).

```js
notifier.sendBinary({
  senderId: "user-alice",
  receiverIds: ["user-bob"],
  buffer: new Uint8Array([0x00, 0x01, 0x02]),
  type: "binary",
});
```

## Receiving messages

Incoming realtime messages arrive on `onMessage`:

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

Reassembled multi-chunk messages are delivered as a single `onMessage` call.

## Errors

| Error                                  | Cause                  |
| -------------------------------------- | ---------------------- |
| `"Not connected"`                      | Call `connect()` first |
| `"senderId required"`                  | Missing sender         |
| `"receiverId or receiverIds required"` | No recipient           |
