Websocket understanding ?

tanmay
I am relatively new to python and trying to study zerodha api documentation in detail.
When going through websocket documentation, I came across this code.
def on _ticks seems that it is bringing out tick data from zerodha and def_connect is letting us subscribe to trading symbols.

In such a case, why does on_ticks appears first ? Shouldn't the connection (subscription to symbol) take place first and only then tick function should appear?

def on_ticks(ws, ticks):
print(ticks)

logging.debug("Ticks: {}".format(ticks))

def on_connect(ws, response):
ws.subscribe([260105])

ws.set_mode(ws.MODE_FULL, [260105])

What can be the reason for this ?
  • sujith
    These are callbacks. The way it works is a user will call connect using the library functions of ticker/Kite Connect. Once connected, you will receive a callback if you have assigned any method to on_connect. Basically, you will write your subscribe code here which will subscribe for ticks after websocket connection is established successfully.
    Once a user sends a subscribe message to the server, it will send the data to which a user will listen to by implementing on_tick.

    PS: Make sure you never block the thread by doing your data dump or calculations inside the on_ticks. It might result in losing the connection or missing ticks. You need to create another thread or offload all the tasks to the secondary or slave thread.
Sign In or Register to comment.