Unable to get live market data stream

Hi Team,

I am unable to get live tick data. I get the initial response about the open market segments but really nothing after that. any guidance would help.

import websocket
import threading
import time
import uuid
import requests
import ssl
import json
from upstox_client import ApiClient, Configuration
from MarketDataFeedV3_pb2 import Feed # Ensure this is your compiled protobuf file
from google.protobuf import json_format

======= CONFIGURATION =======

ACCESS_TOKEN = “REPLACE_WITH_YOUR_ACCESS_TOKEN”
INSTRUMENT_KEYS = [“NSE_FO|50917”] # Example: NIFTY CE known to be active
MODE = “ltpc”

======= STEP 1: FETCH WSS URL =======

def fetch_websocket_url(access_token):
headers = {
“Authorization”: f"Bearer " + access_token,
“Accept”: “application/json”
}
r = requests.get(“https://api.upstox.com/v3/feed/market-data-feed/authorize”, headers=headers)
if r.status_code != 200:
raise Exception(f"Failed to authorize feed. Response: {r.text}")
return r.json()[“data”][“authorized_redirect_uri”]

======= STEP 2: CONNECT TO WSS =======

def start_feed(access_token):
ws_url = fetch_websocket_url(access_token)

def on_open(ws):
    sub_msg = {
        "guid": str(uuid.uuid4()),
        "method": "sub",
        "data": {
            "mode": MODE,
            "instrumentKeys": INSTRUMENT_KEYS
        }
    }
    print("[OPEN] Subscribing to instruments...")
    ws.send(json.dumps(sub_msg))

def on_message(ws, message):
    print(f"[MESSAGE RECEIVED] Binary size: {len(message)}")
    try:
        feed = Feed()
        feed.ParseFromString(message)
        decoded = json.loads(json_format.MessageToJson(feed))
        print("[TICK]", json.dumps(decoded, indent=2))
    except Exception as e:
        print("[ERROR decoding]", e)
        print("[RAW BYTES]", message[:50])

def on_error(ws, error):
    print("[ERROR]", error)

def on_close(ws, code, reason):
    print(f"[CLOSE] code={code}, reason={reason}")

ws_app = websocket.WebSocketApp(
    ws_url,
    on_open=on_open,
    on_message=on_message,
    on_error=on_error,
    on_close=on_close
)

thread = threading.Thread(
    target=lambda: ws_app.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
)
thread.start()

try:
    while True:
        time.sleep(5)
except KeyboardInterrupt:
    print("[EXIT] Closing connection...")
    ws_app.close()
    thread.join()

if name == “main”:
start_feed(ACCESS_TOKEN)

@RISHIKUL_29593713 Please refer to the sample WebSocket example for guidance on properly encoding and decoding the payload.

See if this helps resolve the issue.