Tutorial 3 of 5

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

8 min read~2 min video
STEP 01

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.

STEP 02

WebSocket vs. HTTP — Practical Trade-offs

WebSocket ("ws")HTTP ("http")
ConnectionStays open for the sessionOpens and closes per request
Best forRealtime push (chat, live notifications, streaming events)One-off request/response (AI calls, RAG/knowledge base queries)
Receiving unsolicited messagesYes, via a message callbackNo — you only get a response to what you asked for
Infrastructure fitNeeds a persistent-connection-friendly environmentWorks well behind typical proxies, load balancers, serverless functions
STEP 03

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.
STEP 04

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 ws
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: "user-123",
  WebSocketImpl: WebSocket,
  onConnected: () => {},
  onMessage: () => {},
  onDisconnected: () => {},
});
STEP 05

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: () => {},
});
STEP 06

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",
)
STEP 07

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.

STEP 08

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

Tutorial 3 of 5

  1. Tutorial 1How to Create a New Project
  2. Tutorial 2How to Install the DNotifier SDK
  3. Tutorial 3How to Choose a Transport
  4. Tutorial 4How to Make Your First Connection
  5. Tutorial 5How to Send and Receive Your First Message