web socket dynamic subscription

Guhan
@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
    Hi,
    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")
    kws.subscribe(instrument_token_list)

    Will the code work in Python?
  • rakeshr
    @enigma
    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
    @Guhan
    Do you have any specific condition to subscribe to the specific list of instrument token?
    You can try something below:
    Eg.
    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
    @Guhan
    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?
  • Guhan
    Guhan edited January 28
    @sujith, @rakeshr, @zartimus
    def on_ticks(ws, ticks):  
    ## insert ticks
    def on_connect(ws, response): # noqa
    ins_token_list = get_ins_token_list_func()
    ws.subscribe(ins_token_list)
    ws.set_mode(ws.MODE_FULL,ins_token_list)

    kws.on_ticks = on_ticks
    kws.on_connect = on_connect

    kws.connect()
    in the above example if I want to to add an instrument token to live Websocket I have to manually restart the code to receive new ins_token list from get_ins_token_list_func(). Is there any other way to do it? is it possible to alter subscription?
  • sujith
    The Kite Ticker module of pykiteconnect exposes a method called subscribe, you can keep the instance of ticker in your code and call the method.

    on_connect is only called once after a new connection is established. You don't actually need to kill connection and re-connect in order to subscribe for a bunch of tokens.
  • Guhan
    @sujith, thanks for replying, but still couldn't figure it out, it is driving me crazy here, is there an example or something?
  • rakeshr
    @Guhan
    As @sujith said, you can use subscribe and unsubscribe method for subscribing/unsubscribing token in WebSocket connection.Eg:
    def on_ticks(ws, ticks):
    if(condition for instrument):
    ws.subscribe(new token/list of tokens)
    ws.unsubscribe(remove old token/list of old tokens,if not required)
  • Guhan
    Guhan edited February 4
    Thanks guys I will try that
Sign In or Register to comment.