How to Choose a Transport
Pick WebSocket or HTTP when you initialize DNotifier, based on whether you need realtime push or request/response patterns.
WebSocket vs. HTTP
Understand What "Transport" Means
When you create a DNotifier client, you choose a transport: "ws" (WebSocket) or "http". This is set once, when you initialize the client — it isn't something you switch back and forth on the same instance mid-session.
WebSocket vs. HTTP — Practical Trade-offs
| WebSocket ("ws") | HTTP ("http") | |
|---|---|---|
| Connection | Stays open for the session | Opens and closes per request |
| Best for | Realtime push (chat, live notifications, streaming events) | One-off request/response (AI calls, RAG/knowledge base queries) |
| Receiving unsolicited messages | Yes, via a message callback | No — you only get a response to what you asked for |
| Infrastructure fit | Needs a persistent-connection-friendly environment | Works well behind typical proxies, load balancers, serverless functions |
Decide Based on Your Use Case
Ask yourself: "Does my app need to receive unsolicited messages from DNotifier, or does it only ever ask-and-wait?"
- Building a chat UI, live notification feed, or anything with real-time push → choose WebSocket.
- Building a one-shot AI/RAG integration, a backend job, or something running in a short-lived serverless function → choose HTTP.
- Not sure yet, or building a demo/tutorial project → WebSocket is the safer default since it covers both realtime and request/response patterns.
Node.js — WebSocket Implementation
Browsers provide WebSocket globally. Node.js does not. When using transport: "ws" from a Node.js backend, install a WebSocket package and pass it in as WebSocketImpl:
For transport: "http", no WebSocket package is required.
npm install wsimport 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: "user-123",
WebSocketImpl: WebSocket,
onConnected: () => {},
onMessage: () => {},
onDisconnected: () => {},
});Browser — Native WebSocket
In the browser, omit WebSocketImpl — the SDK uses the native WebSocket API automatically:
import { DNotifier } from "@dnotifier-realtime/dnotifier";
const notifier = new DNotifier({
appId: import.meta.env.VITE_DNOTIFIER_APP_ID,
secret: import.meta.env.VITE_DNOTIFIER_SECRET,
transport: "ws",
userId: "user-123",
onConnected: () => console.log("Connected"),
onMessage: (msg) => console.log(msg.payload.toJSON()),
onDisconnected: () => {},
});Python — Setting the Transport
The Python SDK uses the same transport option, passed as a constructor argument:
notifier = DNotifier(
app_id="your-app-id",
secret="your-app-secret",
transport="ws", # or "http"
user_id="user-123",
)Connect and Confirm the Active Transport
Call your SDK's connect method (notifier.connect() in both JS and Python).
Watch your onConnected / on_connected callback fire — that confirms the chosen transport successfully established a session.
On WebSocket, the connection should stay open (no repeated connect/disconnect cycles) while your app is idle.
On HTTP, there's no persistent "connected" state in the same way — each call completes and returns independently.
Test Both Directions
Send a message (notifier.send(...)) and confirm it's delivered.
- On WebSocket, have a second client/session receive it via the onMessage / on_message callback without polling — that's the behavior you're paying for with WS.
- On HTTP, confirm a request/response call (e.g., an AI call) completes and returns data directly from the call itself.
End of Tutorial Checklist
- Understand the practical difference between WebSocket and HTTP transport
- Chose a transport based on your app's actual use case (push vs. request/response)
- Set transport correctly in the client constructor
- (Node.js + WebSocket only) Installed ws and passed it as WebSocketImpl
- (Browser + production) Avoided embedding the app secret in the frontend bundle
- Verified the connection/response behavior matches the chosen transport
Watch: How to Choose a Transport
Same steps as above — use whichever format you prefer.
DNotifier Tutorial