How to add new instrument without closing the connection?

gully
Hi,

How to add new instrument without closing the connection? I am using the python client.

I am currently creating a thread where on new instrument addition I am connecting again. Is there any other way I can handle this?This would take some time for the new data of the subscribed scrip to come up.

Thanks,Gully
  • Vivek
    You can connect to client with option threaded=True which opens the connection in separate thread and main thread can be used to subscribe to token dynamically. Here is an example
    from kiteconnect import WebSocket

    # Initialise.
    kws = WebSocket("kitefront", "b492a63be6dba6d7fcbe096a6302cad9", "DV1973")

    tokens = []

    # Callback for ticks.
    def on_tick(tick, ws):
    print("ticks", tick)

    # Callback for successful connection.
    def on_connect(ws):
    print("connected")

    # Assign the callbacks.
    kws.on_tick = on_tick
    kws.on_connect = on_connect
    kws.connect(threaded=True)

    # wait to establish a connection
    import time
    time.sleep(5)

    # Wait for user input and block the current thread
    while True:
    token = input("Enter token to subscribe : ")
    print("Subscribe to ", token)
    kws.subscribe([token])
  • gully
    Thanks @vivek I did the same think with eventlet but maybe I will refactor to this.One more request the tickdata comes as an array. The problem is with multiple instrument streaming at one time there has to be loop through array to get the data/instruemnt name.This is a performance overhead. Instead if the payload is in dictionary then search time for key:value is much less compared to array search.I understand this might be very trivial but to imagine a situation where I stream 100 instrument and I expect each instrument has its own handler,then after I receive the tickdata I have to loop through every instrument to invoke the handler. This is costly.
  • Vivek
    @gully Yeah you got a point, If you wish you can write a patch for it and create a pull request else open an issue in github and we will try to incorporate when we get time :) .
This discussion has been closed.