Hi, Can anyone know how to close the websocket. I have search for it but not find any solution except kill the whole program manually. Thanks in advance for the solution
For the time being there is no way to close websocket you can try running websocket as a thread and killing the thread in case you don’t want to kill the whole program
I am not sure if that works, I created a thread running the symbol feed process on foreground and even when I set the killing event for thread, it kept running, I believe the fyers thread is still running behind the scene.
Even I am not sure how to add/delete symbols for a connection running on thread, can you please provide any code for that?
def custom_message1(msg):
print (f"Custom:{msg}")
# condition here
data_type = "symbolData"
symbol = ["MCX:GOLD23JUNFUT"]
fs.subscribe(symbol=symbol, data_type=data_type)
def run_process_symbol_data1(access_token):
global fs
data_type = "symbolData"
symbol = ["MCX:CRUDEOIL23JUNFUT"]
fs = ws.FyersSocket(access_token=access_token,run_background=False ,log_path=".")
fs.websocket_data = custom_message1
fs.subscribe(symbol=symbol, data_type=data_type)
symbol = ["NSE:ASIANPAINT-EQ", "NSE:CIPLA-EQ"]
fs.subscribe(symbol=symbol, data_type=data_type)
fs.keep_running()
this could be used to add/delete new symbols.
We are working on adding a graceful close for the websocket so there is not much hassle closing websocket.
fs = None
symbols = ["NSE:NIFTYBANK-INDEX"]
def run_process_symbol_data(symbols):
global fs
accessToken = f"{APP_ID}:{FyersAccessToken}"
data_type = "symbolData"
symbol = symbols
fs = ws.FyersSocket(access_token=accessToken, log_path=".", run_background=False)
fs.websocket_data = custom_message
fs.subscribe(symbol=symbol, data_type=data_type)
def custom_message(msg):
global fs
format_data(msg[0])
if datetime.now() > datetime.now().replace(hour=15, minute=30):
fs.stop_running()
sys.exit()