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
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/dnotifierFor WebSocket transport, also install the ws package:
npm install wsWebSocketImpl 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
wsbundled 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:
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.
Typical Node.js architectures
Realtime gateway service
A long-running Node process holds a WebSocket connection per logical user or device:
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:
Graceful shutdown
Close the socket when your process exits:
Debugging tips
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