web socket dynamic subscription

@sujith, is it possible to dynamically subscribe and unsubscribe tokens in live web socket connection?
If possible can you explain me with an example
  • sujith
    You can call subscribe and unsubscribe at any point of time.
    There is no special call for that. It is just the normal subscribe you use when you start Kite Ticker connection.
  • enigma
    enigma edited December 2018
    I am trying to do the same thing but the problem is I am little confused how to make this work.
    For example I am streaming NIFTY 50 and while the connection is live, I want to add another instrument. So if I want to add it you are saying that I need to use the subscribe() function to add the new instrument.

    So with my understanding, will the below code snippet work??
    kws = KiteTicker("your_api_key", "your_access_token")

    Will the code work in Python?
  • rakeshr
    Yeah, you need to use kws.subscribe(token/list of tokens) in on_connect method to subscribe to new instrument each time.
  • Guhan
    @rakeshr how to change the subscription list or call kms.subscribe in a live streaming Websocket. I still couldn't figure it out.
  • sujith
    You can call unsubscribe for the tokens which you don't want to listen to.
  • sujith
    You can check out Kite Connect documentation.
  • rakeshr
    Do you have any specific condition to subscribe to the specific list of instrument token?
    You can try something below:
    def on_connect(ws, response): 
    if(condition for instrument):
    ws.subscribe(token/list of token)
    ws.set_mode(ws.MODE_FULL,token/list of token)
  • Guhan
    @sujith, @rakeshr thanks for replying. I will explain how I am doing it, I start web socket and screener code in the morning, the web socket will have predefined subscription list based on my live positions. When screener code finds potential trades I want it to add those instruments to the live websocket code without interrupting the data-feed.
  • rakeshr
    Yeah, you can use above sample code for condition-based token subscription.
  • Guhan
    @rakeshr once web socket code starts running control doesn't go to on_connect, it will be checked only once right?
Sign In or Register to comment.