I have written the class as
class WebSocket: def __init__(self) -> None: self.fyers_authenticator = FyersAuthenticator() # self.access_token = f'{client_id}-{app_type}:{self.fyers_authenticator.generate_access_token()}' self.access_token = self.fyers_authenticator.generate_access_token() self.fyers_order_ws = self.create_fyers_order_ws() self.fyers_data_ws = self.create_fyers_data_ws() def onTrade(self, message): """ Callback function to handle incoming messages from the FyersDataSocket WebSocket. Parameters: message (dict): The received message from the WebSocket. """ logger.info("Trade Response:", message) def onOrder(self, message): """ Callback function to handle incoming messages from the FyersDataSocket WebSocket. Parameters: message (dict): The received message from the WebSocket. """ logger.info("Order Response:", message) def onPosition(self, message): """ Callback function to handle incoming messages from the FyersDataSocket WebSocket. Parameters: message (dict): The received message from the WebSocket. """ logger.info("Position Response:", message) def onGeneral(self, message): """ Callback function to handle incoming messages from the FyersDataSocket WebSocket. Parameters: message (dict): The received message from the WebSocket. """ logger.info("General Response:", message) def onerror(self, message): """ Callback function to handle WebSocket errors. Parameters: message (dict): The error message received from the WebSocket. """ logger.error("Error:", message) def onclose(self, message): """ Callback function to handle WebSocket connection close events. """ logger.info("Connection closed:", message) def onmessage(message): """ Callback function to handle incoming messages from the FyersDataSocket WebSocket. Parameters: message (dict): The received message from the WebSocket. """ logger.info("Response:", message) def onopen_data(self): """ Callback function to subscribe to data type and symbols upon WebSocket connection. """ # Specify the data type and symbols you want to subscribe to data_type = "SymbolUpdate" # data_type = "DepthUpdate" # Subscribe to the specified symbols and data type symbols = ['NSE:SBIN-EQ'] self.fyers_data_ws.subscribe(symbols=symbols, data_type=data_type) # Keep the socket running to receive real-time data self.fyers_data_ws.keep_running() def onopen_order(self): """ Callback function to subscribe to data type and symbols upon WebSocket connection. """ # Specify the data type and symbols you want to subscribe to # data_type = "OnOrders" # data_type = "OnTrades" # data_type = "OnPositions" # data_type = "OnGeneral" data_type = "OnOrders,OnTrades,OnPositions,OnGeneral" self.fyers_order_ws.subscribe(data_type=data_type) # Keep the socket running to receive real-time data self.fyers_order_ws.keep_running() def create_fyers_data_ws(self): # Create a FyersDataSocket instance with the provided parameters return data_ws.FyersDataSocket( access_token=self.access_token, # Access token in the format "appid:accesstoken" log_path="", # Path to save logs. Leave empty to auto-create logs in the current directory. litemode=False, # Lite mode disabled. Set to True if you want a lite response. write_to_file=False, # Save response in a log file instead of printing it. reconnect=True, # Enable auto-reconnection to WebSocket on disconnection. on_connect=self.onopen_data, # Callback function to subscribe to data upon connection. on_close=self.onclose, # Callback function to handle WebSocket connection close events. on_error=self.onerror, # Callback function to handle WebSocket errors. on_message=self.onmessage # Callback function to handle incoming messages from the WebSocket. ) def connect_to_data_ws(self): # Connect to the FyersDataSocket WebSocket self.fyers_data_ws.connect() def create_fyers_order_ws(self): # Create a FyersDataSocket instance with the provided parameters return order_ws.FyersOrderSocket( access_token=self.access_token, # Your access token for authenticating with the Fyers API. write_to_file=False, # A boolean flag indicating whether to write data to a log file or not. log_path="", # The path to the log file if write_to_file is set to True (empty string means current directory). on_connect=self.onopen_order, # Callback function to be executed upon successful WebSocket connection. on_close=self.onclose, # Callback function to be executed when the WebSocket connection is closed. on_error=self.onerror, # Callback function to handle any WebSocket errors that may occur. on_general=self.onGeneral, # Callback function to handle general events from the WebSocket. on_orders=self.onOrder, # Callback function to handle order-related events from the WebSocket. on_positions=self.onPosition, # Callback function to handle position-related events from the WebSocket. on_trades=self.onTrade # Callback function to handle trade-related events from the WebSocket. ) def connect_to_order_ws(self): # Connect to the FyersDataSocket WebSocket self.fyers_order_ws.connect()