JavaScript / TypeScript

CommonJS (Node.js with require)

Use this in .js files when your app uses require() (no "type": "module" in package.json):

const { DNotifier } = require("@dnotifier-realtime/dnotifier");
const WebSocket = require("ws");

const notifier = new DNotifier({
  appId: "your_app_id",
  secret: "your_app_secret",
  transport: "ws",
  userId: "current_user_id",
  WebSocketImpl: WebSocket,
  onConnected: () => console.log("Connected"),
  onMessage: (data) => console.log(data.payload.toJSON()),
  onDisconnected: ({ code, reason }) => console.log("Disconnected", code, reason),
});

await notifier.connect();

ESM (Node.js with import) / Node.js with TypeScript

Use this in .mjs files or in a project with "type": "module" in package.json:

React (Vite / Create React App / Next.js client)

In the browser, use the global WebSocket; do not install or pass ws:

If your React app runs in Node (e.g. SSR or a Node-based tool) and uses transport: "ws", pass the ws implementation:

Last updated