ERROR: Websocket connection.

Here is the log: The code does does fetch data, but connection is not getting closed. Please check.

{"level":"ERROR","location":"[On_message:1447] data_ws","message":"cannot join current thread","timestamp":"2024-12-12 14:36:51,376+0530","service":"FyersDataSocket"}

Hey @em2O0KClgr

Could you please DM the code snippet used here

How do I wrap the code in the message for proper indentation here?

import pandas as pd

from fyers_apiv3.FyersWebsocket import data_ws

from common_functions import common_functions as cf

# Initialize the counter

count = 0

limit = 0

db = [cf.read](http://cf.read)_config()

newdf = []

def onmessage(message):

    global count

    # if 'ltp' in message:

    if message['type'] == 'sf':

        count += 1

        cond_a = message['ltp'] > message['open_price']

        

        _dict = {

            'symbol': only_symbol,

            'open'  : message['open_price'],

            'high'  : message['high_price'],

            'low'   : message['low_price'],

            'close' : message['ltp'],

            'chp'   : message['chp'],

            'vol'   : message['vol_traded_today']

        }

        if cond_a:

            newdf.append(_dict)

        # Stop the websocket when count reaches end of symbols list

        if count >= limit:

            fyers.close_connection()

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)

    # do some process.

    print('Done.')

    exit(0)

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"

    df = [pd.read](http://pd.read)_csv(db['stocksfile'])

    # Filter rows where the status column not equals -1

 

    global limit

    limit = len(df)

    # Subscribe to the specified symbols and data type

    # Extract unique symbols

    symbols = df['Symbol'].unique()

    # Prefix "NSE:" and suffix "-EQ"

    symbols_with_prefix_suffix = ['NSE:' + Symbol + '-EQ' for Symbol in symbols]

    fyers.subscribe(symbols=symbols_with_prefix_suffix, data_type=data_type)

    # Keep the socket running to receive real-time data

    fyers.keep_running()

atF = open(r"access_token.txt", "r")

access_token = [atF.read](http://atF.read)()

atF.close()

# 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()

exit(0)