issue while using fyers.subsrcibe() and fyers.unsubscribe()

Dear Fyers team,

I am starting a websocket with predefined symbols and it works as expected,once connection is established i am able to get real time ltps of symbols I have subscribed.

The problem arises when I want to add a new symbol or remove an existing symbol using fyers.subsribe() and fyers.unsubsribe() respectively, as per my observation sometimes the above mentioned methods work as expected (successful subscribe/successfull unsubscribe) but sometimes they fail to subscribe and unsubscribe.

Can you please guide me on how to solve this intermittent issue which i am facing..?

Hey @107165085553102160598 ,

Have checked this from my side and this is working absolutely fine , this maybe due to the implementation , caould you try running the sample code for sockets and try subscribe/unsubscribe of symbols . Also please capture the logs if the issue persists

It works fine once

But when i do it again and again

It fails somehow

Can i please share my code abhishek?

def onclose(message):

"""Handles WebSocket connection close events."""

print("Connection closed:", message)

def onopen():

"""

Callback function to subscribe to data type and symbols upon WebSocket connection.

"""

global subscribed_symbols

# Specify the data type and symbols you want to subscribe to

data_type = "SymbolUpdate"

# Subscribe to the specified symbols and data type

SYMBOLS=set(get_scripts())

print(SYMBOLS)

subscribed_symbols.update(SYMBOLS)

#SYMBOLS = ["MCX:CRUDEOILM25FEBFUT","MCX:SILVER25MARFUT"]

#fyers.subscribe(symbols=SYMBOLS, data_type=data_type)

fyers.subscribe(symbols=list(subscribed_symbols), data_type=data_type)

# Keep the socket running to receive real-time data

fyers.keep_running()

Replace with your actual Fyers access token

#access_token = “R0YJ55A6R4-100:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkuZnllcnMuaW4iLCJpYXQiOjE3NDA2MzY1NzEsImV4cCI6MTc0MDcwMjYzMSwibmJmIjoxNzQwNjM2NTcxLCJhdWQiOlsieDowIiwieDoxIiwieDoyIiwiZDoxIiwiZDoyIiwieDoxIiwieDowIl0sInN1YiI6ImFjY2Vzc190b2tlbiIsImF0X2hhc2giOiJnQUFBQUFCbndBR2JibGcxSmpBVkN4ZXpVS25nNWhSVUx5am1jMDlLbmJiU0h1V1JJREw5YWh0N1VoZXlUOU5HcXRrNFpNNHYxdXZrd0lqcHBMRmhNZDRNXzRTdVFONGFZYWFUQ1ZfYVYxeExrSWhhSUx5cDFkND0iLCJkaXNwbGF5X25hbWUiOiJNSVRVTCBNQU5HRVNIIFNIRUxBVEtBUiIsIm9tcyI6IksxIiwiaHNtX2tleSI6IjUzYWEzMjU4NTg0MWVlOTUzYjk1Mjc4MmJiNDY3ZjYwNzE5ZTgzZjdhZjM1MDJhNzc2YTNhYTEzIiwiaXNEZHBpRW5hYmxlZCI6Ik4iLCJpc010ZkVuYWJsZWQiOiJOIiwiZnlfaWQiOiJZTTE5NTI1IiwiYXBwVHlwZSI6MTAwLCJwb2FfZmxhZyI6Ik4ifQ.c9o2dG_Xa7Hleir3QQ9OJhlJxFtkY23NEDAUgBhZ4Bk”

access_token = os.getenv(“FYERS_ACCESS_TOKEN”, “Token Not Found”)

Initialize Fyers WebSocket

fyers = data_ws.FyersDataSocket(

access_token=access_token,

log_path="",

litemode=False,

write_to_file=False,

reconnect=True,

on_connect=onopen,

on_close=onclose,

on_error=onerror,

on_message=onmessage

)

Start FastAPI Server in a Separate Thread

def run_fastapi():

[uvicorn.run](http://uvicorn.run)(app, host="0.0.0.0", port=8000)

fastapi_thread = threading.Thread(target=run_fastapi, daemon=True)

fastapi_thread.start()

Connect to Fyers WebSocket

shutdown_event = threading.Event()

try:

fyers.connect()

shutdown_event.wait()  # Wait indefinitely until shutdown signal

except KeyboardInterrupt:

print("CTRL + C detected. Closing connection...")

fyers.close_connection()

shutdown_event.set()

print("[INFO] Program terminated.")