Facing error 12001 when invoking fetching data via web sockets using python api

Hi,

I’m running the sample code provided to fetch MCX CRUDE OIL Future data using websockets.

I’m getting the following exception:

Error: {'code': 12001, 'message': 'Mode change failed', 's': 'error'} Error: {'type': 'sub', 'code': 11011, 'message': 'subscription failed', 's': 'error'}

Can you please tell me where I might be wrong. Below is the code as well:

from fyers_apiv3.FyersWebsocket import data_ws

def onmessage(message):
    """
    Callback function to handle incoming messages from the FyersDataSocket WebSocket.

    Parameters:
        message (dict): The received message from the WebSocket.

    """
    print("Response:", message)

def onerror(message):
    """
    Callback function to handle WebSocket errors.

    Parameters:
        message (dict): The error message received from the WebSocket.

    """
    print("Error:", message)

def onclose(message):
    """
    Callback function to handle WebSocket connection close events.
    """
    print("Connection closed:", message)

def onopen():
    """
    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"

    # Subscribe to the specified symbols and data type
    #symbols = ['NSE:SBIN-EQ', 'NSE:ADANIENT-EQ']
    symbols = ['MCX:CRUDEOIL24JULFUT']
    #symbols = ["NSE:NIFTY50-INDEX" , "NSE:NIFTYBANK-INDEX"]
    fyers.subscribe(symbols=symbols, data_type=data_type)

    # Keep the socket running to receive real-time data
    fyers.keep_running()

# Replace the sample access token with your actual access token obtained from Fyers
access_token = "ABCD-100:eXXXXXXXXXXXXfZNSBoLo"
# Removed the actual access_token being used

# Create a FyersDataSocket instance with the provided parameters
fyers = data_ws.FyersDataSocket(
    access_token=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=onopen,               # Callback function to subscribe to data upon connection.
    on_close=onclose,                # Callback function to handle WebSocket connection close events.
    on_error=onerror,                # Callback function to handle WebSocket errors.
    on_message=onmessage             # Callback function to handle incoming messages from the WebSocket.
)

# Establish a connection to the Fyers WebSocket
fyers.connect()

Hey @102270756830919809556

Can we please get the datasocket logs for this

hi,

i am also getting the same error, pls guide

datasocket log:

03 16:17:59,259+0530",“service”:“FyersDataSocket”}

{“level”:“ERROR”,“location”:“[__subscribe_resp:711] data_ws”,“message”:{“type”:“sub”,“code”:11011,“message”:“subscription failed”,“s”:“error”},“timestamp”:“2024-08-03 16:18:01,810+0530”,“service”:“FyersDataSocket”}

{“level”:“ERROR”,“location”:“[__access_token_msg:352] data_ws”,“message”:“object of type ‘NoneType’ has no len()”,“timestamp”:“2024-08-03 16:56:21,342+0530”,“service”:“FyersDataSocket”,“exception”:“Traceback (most recent call last):\n File "C:\Users\Murthy Kadam\AppData\Local\Programs\Python\Python311\Lib\site-packages\fyers_apiv3\FyersWebsocket\data_ws.py", line 307, in __access_token_msg\n buffer_size = 18 + len(self.__hsm_token) + len(self.source)\n ^^^^^^^^^^^^^^^^^^^^^\nTypeError: object of type ‘NoneType’ has no len()”,“exception_name”:“TypeError”}

{“level”:“ERROR”,“location”:“[__lite_full_mode_resp:828] data_ws”,“message”:{“code”:12001,“message”:“Mode change failed”,“s”:“error”},“timestamp”:“2024-08-03 16:56:21,490+0530”,“service”:“FyersDataSocket”}

{“level”:“ERROR”,“location”:“[__subscribe_resp:711] data_ws”,“message”:{“type”:“sub”,“code”:11011,“message”:“subscription failed”,“s”:“error”},“timestamp”:“2024-08-03 16:56:23,671+0530”,“service”:“FyersDataSocket”}

{“level”:“ERROR”,“location”:“[__access_token_msg:352] data_ws”,“message”:“object of type ‘NoneType’ has no len()”,“timestamp”:“2024-08-03 17:01:09,687+0530”,“service”:“FyersDataSocket”,“exception”:“Traceback (most recent call last):\n File "C:\Users\Murthy Kadam\AppData\Local\Programs\Python\Python311\Lib\site-packages\fyers_apiv3\FyersWebsocket\data_ws.py", line 307, in __access_token_msg\n buffer_size = 18 + len(self.__hsm_token) + len(self.source)\n ^^^^^^^^^^^^^^^^^^^^^\nTypeError: object of type ‘NoneType’ has no len()”,“exception_name”:“TypeError”}

{“level”:“ERROR”,“location”:“[__lite_full_mode_resp:828] data_ws”,“message”:{“code”:12001,“message”:“Mode change failed”,“s”:“error”},“timestamp”:“2024-08-03 17:01:09,905+0530”,“service”:“FyersDataSocket”}

{“level”:“ERROR”,“location”:“[__subscribe_resp:711] data_ws”,“message”:{“type”:“sub”,“code”:11011,“message”:“subscription failed”,“s”:“error”},“timestamp”:“2024-08-03 17:01:12,008+0530”,“service”:“FyersDataSocket”}