I am using Flask gunicorn to connect to upstox websocket and fetch live market data. The example I have followed is https://github.com/upstox/upstox-python/blob/master/examples/websocket/market_data/websocket_client.py
Instead of using async I have used threading as flask/python does not support async
My code
def __decode_protobuf(buffer):
"""Decode protobuf message."""
feed_response = pb.FeedResponse()
feed_response.ParseFromString(buffer)
return feed_response
def __fetch_market_data(ws_url,instrument_list):
with tracer.start_as_current_span(
f"OHLC_Feed_monitor_{instrument_list}"
):
# Create SSL context
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
try:
# Connect to WebSocket with SSL
log.info(f"Connecting to WebSocket at {ws_url}")
websocket = create_connection(ws_url, sslopt={"cert_reqs": ssl.CERT_NONE})
log.info("Connection established")
time.sleep(1) # Wait for a moment before sending data
data = {
"guid": f"{int(datetime.utcnow().timestamp())}",
"method": "sub",
"data": {
"mode": "ltpc",
"instrumentKeys": ["NSE_EQ|INE002A01018"]
}
}
log.info(data)
# Send subscription message
binary_data = json.dumps(data).encode('utf-8')
websocket.send(binary_data)
# Continuously receive and decode data from WebSocket
while True:
try:
message = websocket.recv()
decoded_data = __decode_protobuf(message)
data_dict = MessageToDict(decoded_data)
log.info(f"Received: {data_dict}")
# websocket_data.append(message) # Store data in shared list
except WebSocketConnectionClosedException:
log.error("WebSocket connection closed")
break
except Exception as e:
log.error(f"Error: {e}")
break
finally:
if websocket:
websocket.close()
log.info("WebSocket thread exiting")
Although the authorization is happening properly and connection is being established, there is no data during live market hours. The only exchange I see is the standard ping-pong to keep the connection alive
App Debug logs
2024-12-27 10:52:38 ++Sent raw: b'\x8a\x80\xb0^\xa4\x9d'
2024-12-27 10:52:38 DEBUG:websocket:++Sent raw: b'\x8a\x80\xb0^\xa4\x9d'
2024-12-27 10:52:38 ++Sent decoded: fin=1 opcode=10 data=b''
2024-12-27 10:52:38 DEBUG:websocket:++Sent decoded: fin=1 opcode=10 data=b''
2024-12-27 10:52:43 ++Rcv raw: b'\x89\x00'
2024-12-27 10:52:43 DEBUG:websocket:++Rcv raw: b'\x89\x00'
2024-12-27 10:52:43 ++Rcv decoded: fin=1 opcode=9 data=b''
I tried the same through postman too and the same behaviour is happening. Although connection is establishing there is no data (you can see the different payloads I am experiencing with)