> 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/structured-payloads/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 images, audio, and documents via `send()` with typed `data` objects.

***

## Image

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

const imageBytes = readFileSync("./photo.png");

const notifier = new DNotifier({
  appId: process.env.DNOTIFIER_APP_ID,
  secret: process.env.DNOTIFIER_SECRET,
  transport: "ws",
  userId: "user-alice",
  WebSocketImpl: WebSocket,
  onConnected: () => {},
  onMessage: (msg) => {
    const body = msg.payload.toJSON();
    if (body?.type === "image") {
      console.log("Received image, bytes:", body.content?.length);
    }
  },
  onDisconnected: () => {},
});

await notifier.connect();

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

### Browser image from file input

```js
const file = document.querySelector("input[type=file]").files[0];
const arrayBuffer = await file.arrayBuffer();
const imageBytes = new Uint8Array(arrayBuffer);

await notifier.send({
  senderId: myUserId,
  receiverId: peerUserId,
  data: {
    type: "image",
    content: imageBytes,
    fileName: file.name,
    mimeType: file.type,
  },
});
```

***

## Audio

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

const audioBytes = readFileSync("./voice-note.m4a");

await notifier.send({
  senderId: "user-alice",
  receiverId: "user-bob",
  data: {
    type: "audio",
    content: audioBytes,
    durationMs: 12500,
    mimeType: "audio/mp4",
  },
});
```

***

## Document

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

const docBytes = readFileSync("./report.pdf");

await notifier.send({
  senderId: "user-alice",
  receiverId: "user-bob",
  data: {
    type: "doc",
    content: docBytes,
    fileName: "report.pdf",
    mimeType: "application/pdf",
  },
});
```

***

## Receiving and displaying

```js
onMessage: (msg) => {
  const body = msg.payload.toJSON();

  switch (body?.type) {
    case "image": {
      const blob = new Blob([body.content], { type: body.mimeType ?? "image/png" });
      const url = URL.createObjectURL(blob);
      showImagePreview(url);
      break;
    }
    case "audio":
      playAudioBuffer(body.content, body.mimeType);
      break;
    case "doc":
      offerDownload(body.content, body.fileName, body.mimeType);
      break;
  }
},
```

***

## Size limits

Check `notifier.messageSizeLimit` after `connect()`. Payloads larger than this limit are **chunked automatically** — you do not split files manually.

```js
await notifier.connect();
console.log("Max chunk size:", notifier.messageSizeLimit);
```

→ [Large messages & chunking](https://dnotifier.gitbook.io/product-docs/realtime-communication/chunking.md)

***

## Learn more

* [Binary messaging](https://dnotifier.gitbook.io/product-docs/realtime-communication/binary-messaging/javascript-typescript.md)
* [Chat UI patterns](https://dnotifier.gitbook.io/product-docs/chat/ui-patterns.md)
