Browse documentation
Guides

How to Set Up WebSockets

This guide walks you through receiving your first WebSocket delivery end-to-end: copy an API key, install the SDK, subscribe to a product, and confirm events arrive. By the end you'll have a production-ready subscriber pattern.

Prerequisites

  • Starter, Pro, or Scale plan. Upgrade if needed. Sandbox does not include live WebSocket streams.
  • Node.js 18+ or Python 3.10+ for the code samples.
  • A Drishti API key from the platform console.

What we'll build

Throughout this guide we'll build a subscriber for a fictional integrator, Acme Capital. Acme wants a console log the moment Drishti publishes a new corporate announcement for any ticker on their watchlist (RELIANCE, TCS, INFY).

Value
Acme's watchlistRELIANCE, TCS, INFY
Event typeannouncements
Endpointwss://developers.manasija.in/v1/ws
Delivery modedetailed: true

Substitute your own symbols and products as you follow along.

Step 1: Copy your API key

  1. Open the Drishti platform console.
  2. Sign in and go to the developer portal.
  3. Copy an existing API key or create a new one for your application.
  4. Store it in your environment — never commit it to source control.
bash
export DRISHTI_API_KEY=your-api-key-here

Step 2: Install the SDK

The SDK websocket session client connects in the background, replays subscriptions after every reconnect, and keeps retrying until you call close().

JavaScript / TypeScript

pnpm add drishti-sdk

Python

bash
pip install drishti-sdk

Step 3: Connect and subscribe

The subscriber does three things: authenticate on connect, send a subscribe message for announcements, and print the acknowledgement.

JavaScript / TypeScript

ts
import { DrishtiClient } from "drishti-sdk"

const WATCHLIST = ["RELIANCE", "TCS", "INFY"]

const client = new DrishtiClient({ apiKey: process.env.DRISHTI_API_KEY! })
const ws = client.websocket()

const subscription = await ws.subscribe({
  product: "announcements",
  symbols: WATCHLIST,
  detailed: true,
})

console.log("subscribed", subscription.product, subscription.tier)

Python

python
import asyncio
import os
from drishti_sdk import DrishtiClient

WATCHLIST = ["RELIANCE", "TCS", "INFY"]

async def main() -> None:
    client = DrishtiClient(api_key=os.environ["DRISHTI_API_KEY"])
    async with client.websocket() as ws:
        ack = await ws.subscribe("announcements", symbols=WATCHLIST, detailed=True)
        print("subscribed", ack.product, ack.tier)
        await asyncio.Event().wait()

asyncio.run(main())

Run the script. You should see a line like subscribed announcements starter_100 (your tier label depends on plan).

Raw WebSocket (optional)

If you cannot use the SDK, open a raw socket and send JSON subscribe frames yourself. You are responsible for reconnecting and resubscribing.

text
wss://developers.manasija.in/v1/ws?api_key=<key>
json
{"op":"subscribe","product":"announcements","symbols":["RELIANCE","TCS","INFY"],"detailed":true}

Step 4: Handle incoming events

Wire up a handler that filters to Acme's watchlist and logs the summary. In production you would enqueue this work and keep the handler fast.

JavaScript / TypeScript — event listeners

ts
const WATCHLIST = new Set(["RELIANCE", "TCS", "INFY"])

const ws = client.websocket({
  onAnnouncements: (announcement) => {
    if (!WATCHLIST.has(announcement.symbol)) return
    console.log(`watchlist hit: ${announcement.symbol} — ${announcement.summary}`)
  },
})

await ws.subscribe({ product: "announcements", symbols: [...WATCHLIST], detailed: true })
// listeners fire as events arrive; call await ws.close() when finished

JavaScript / TypeScript — async iterator

ts
const ws = client.websocket()
await ws.subscribe({ product: "announcements", symbols: ["RELIANCE", "TCS", "INFY"] })

for await (const event of ws.events()) {
  if (event.kind === "subscribed") {
    console.log(event.product, event.tier)
    continue
  }
  if (event.kind === "data" && event.channel === "announcements") {
    const row = event.data
    console.log(`announcement ${row.symbol}: ${row.summary}`)
  }
  if (event.kind === "error") console.error(event.message)
}

Python — event listeners

python
WATCHLIST = {"RELIANCE", "TCS", "INFY"}

def on_announcement(row: dict) -> None:
    symbol = row.get("symbol")
    if symbol not in WATCHLIST:
        return
    print(f"watchlist hit: {symbol} — {row.get('headline')}")

async with client.websocket(on_announcements=on_announcement) as ws:
    await ws.subscribe("announcements", symbols=sorted(WATCHLIST), detailed=True)
    await asyncio.Event().wait()

When a new filing lands for RELIANCE, TCS, or INFY, Acme's handler should log a watchlist hit line. Events for other symbols are ignored.

Production checklist

Before flipping real customer flows onto your subscriber:

  • Use the SDK session client so reconnects replay subscriptions automatically.
  • Keep handlers fast — enqueue heavy parsing, LLM calls, and notifications to a background worker.
  • Watch symbol caps — Starter allows 100 active symbols account-wide; Pro allows 1,000.
  • Rotate the API key if it leaks. Create a new key in the platform console and deploy the change in lockstep.

Common pitfalls

Connection rejected on Sandbox. Live WebSocket streams require a paid plan. Upgrade to Starter or above.

Subscribe succeeds but no events arrive. Confirm your symbols are listed on NSE/BSE and that recent filings exist. Quiet markets produce quiet sockets.

Events stop after a network blip. Raw sockets lose subscriptions on disconnect. Switch to the SDK or resubscribe manually after every reconnect.

API key visible in the browser. Query-string auth is convenient for local testing but unsafe for production user-facing apps. Proxy through your backend.

Next steps