Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Node.js

Use the DNotifier JavaScript SDK in Node.js for backend services, workers, CLI tools, and server-side realtime or AI integrations. Node has no built-in WebSocket client — you must inject one when using transport: "ws".

Requirements

Requirement
Version / notes

Node.js

18 or later recommended

npm / yarn / pnpm

Any recent version

DNotifier credentials

appId and secret from app.dnotifier.com

→ Install — JavaScript / TypeScript


Install

npm install @dnotifier-realtime/dnotifier

For WebSocket transport, also install the ws package:

npm install ws

The ws package is a peer-style dependency for Node WebSocket use. It is listed as a dependency of the SDK but you import and pass it explicitly as WebSocketImpl.


WebSocketImpl and the ws package

Browsers expose WebSocket globally. Node.js does not. The SDK's WebsocketTransport accepts a constructor via WebSocketImpl:

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: "backend-worker-1",
  WebSocketImpl: WebSocket,
  onConnected: () => console.log("Connected"),
  onMessage: (msg) => console.log(msg.payload.toJSON()),
  onDisconnected: () => console.log("Disconnected"),
});

await notifier.connect();

Why injection?

  • Keeps the SDK bundle small in browser builds (no ws bundled for the web)

  • Lets you use a compatible WebSocket implementation in tests or custom environments

  • Matches Node ESM/CJS patterns — you control the import

What happens without WebSocketImpl?

If transport: "ws" and no WebSocketImpl is passed, the SDK throws:


HTTP transport (no WebSocket)

Server-side AI assistants, cron jobs, and workflow runners typically use HTTP:

→ Choose your transport


ESM and CommonJS

The package is ESM-first ("type": "module"):

Legacy CommonJS:

TypeScript types ship in dist/dnotifier.d.ts — no separate @types package.


Environment variables

Load credentials from the environment, never hard-code secrets:

Use .env with dotenv in development; use your host's secret manager in production.

→ Credentials & environment


Typical Node.js architectures

Realtime gateway service

A long-running Node process holds a WebSocket connection per logical user or device:

Piece
Transport
Role

Chat relay

ws

Forward messages between user IDs

AI API route

http

sendAI, search, runWorkflow

Many teams run two SDK instances — one per transport — sharing the same appId and secret.

Worker / queue consumer

A worker connects as a service userId, receives jobs via onMessage, and responds with send():

Serverless note


Custom WebSocket URL

Override the WebSocket URL only when DNotifier has provided a dedicated host:

→ Endpoints & custom URL


Graceful shutdown

Close the socket when your process exits:

→ Connection lifecycle


Debugging tips

Symptom
Check

No WebSocket implementation provided

Add WebSocketImpl: WebSocket

Authentication Failed

Verify appId / secret in dashboard

Connection drops under load

Review plan limits and chunk sizes

WebSocket not connected on send()

Await connect() before sending

→ Debugging · Troubleshooting


Next steps

Last updated