Getting No Data When I am putting Websocket code in Python class

sudheer1990
from kiteconnect import WebSocket
#import pandas as pd

class GetWebsocketData:

def __init__(self, user_auth):
self.kws = WebSocket(user_auth.api_key, user_auth.public_token, user_auth.user_id)

def on_tick(tick, ws):
print("Sudheer")
print(type(tick[0]))
print(len(tick[0]))
#print(pd.Panel(tick[0])['ohlc'])
for x, y in tick[0].items():
print("Key - Value ", x, y)

def on_connect(ws):
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
ws.subscribe([5633])

# Set RELIANCE to tick in `full` mode.
#self.kws.set_mode(ws.MODE_FULL, [5633])


# Assign the callbacks.
def assign_callback(self):
self.kws.on_tick = self.on_tick
self.kws.on_connect = self.on_connect
self.kws.connect()



getwebsocket = GetWebsocketData(user_auth)
getwebsocket.assign_callback()


user_auth object reference contains api_key, public_token, user_id..

Can you please tell me what is the problem here
  • sudheer1990
    no I am on windows .....using spyder(python) enviornment
  • Kailash
    Okay. What version is your Python?
  • sudheer1990
    for me below is working .......but when I am trying to put below code in class, this is not working ....

    do we need to handle the callback kind of thing in special way ..while using in class?



    public_token = "xxxxxx"
    api_key="xxxx"
    user_id = "xxx"

    #--------------------------------------------------#
    # Initialise.
    kws = WebSocket(api_key, public_token, user_id)

    # Callback for tick reception.
    def on_tick(tick, ws):
    print("Sudheer")
    print(type(tick[0]))
    print(len(tick[0]))
    #print(pd.Panel(tick[0])['ohlc'])
    for x, y in tick[0].items():
    print("Key - Value ", x, y)


    #data_dict = tick[0]
    # print(data_dict)
    #data = pd.DataFrame(data_dict)
    #print(data)
    #print(pd.DataFrame(eval(tick)['ohlc'], columns=["open", "high", "low", "close"]))



    # Callback for successful connection.
    def on_connect(ws):
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
    ws.subscribe([5633])

    # Set RELIANCE to tick in `full` mode.
    #ws.set_mode(ws.MODE_FULL, [5633])

    # Assign the callbacks.
    kws.on_tick = on_tick
    kws.on_connect = on_connect

    # Infinite loop on the main thread. Nothing after this will run.
    # You have to use the pre-defined callbacks to manage subscriptions.
    kws.connect()
  • Kailash
    I am not sure what's wrong here. If the sample code is working fine and your class based code isn't, there's an issue with the class implementation at your end.
  • sudheer1990
    I wanted to know that whether above code for websocket support class based implementation or not ?

    If it support, then i guess the code that I have written should work ?
Sign In or Register to comment.