Enquiry about KiteTicker connection

nkotwar
Does connection to ticker closes when token expires?
  • SRIJAN
    SRIJAN edited April 2022
    No, as websocket is a single long standing connection. You just need access token to start websocket.
    However,you won't be able to make any other api calls if access token expires.
  • nkotwar
    gotcha, Thanks.
  • nkotwar
    nkotwar edited April 2022
    @SRIJAN
    Say I established a websocket connection, everything going smooth, next day, after access_token which I used to create connection in 1st place expires, as I understand, connection will still be smooth.
    My query is, if there's a connection error next day, when access token has changed, does auto reconnect work as expected?
    or will I need to pass in new access token to create websocket object again and connect again?
  • SRIJAN
    SRIJAN edited April 2022
    You will need a valid access token to connect to websocket every time.
    So,reconnection won't work in case access token expires. It will give error 403 .
    Hence,if you are confident there will be no disconnections,then only continue without generating new access token every morning.
  • nkotwar
    nkotwar edited April 2022
    So,reconnection won't work in case access token expires. It will give error 403 .
    whoa!
    I'm glad I asked this query, this is very valuable info.
    if you are confident there will be no disconnections
    Even though I have everything hosted on AWS, still once a week i was seeing 403, which was annoying, I thought why is auto reconnect not working!
  • nkotwar
    nkotwar edited April 2022
    @SRIJAN
    Now, I'm not sure how should I handle connection drop at some time after access token was expired and updated in global variable, is this correct way to go about it?


    def on_close(ws, code, reason):
    ....ticks_logger.info(f"Connection Closed !! Reason: {reason}")
    ....global kws, api_key, access_token

    ....if kws==None or kws.is_connected()==False:
    ........kws = KiteTicker(api_key, access_token)
    ........kws.connect(threaded=True)


    Isn't this defeating the purpose of auto re connection?
  • SRIJAN
    SRIJAN edited April 2022
    Yeah,it looks ok. Just one thing you have to add.
    Before initializing new KiteTicker instance inside on_close, use ws.stop_retry() ,otherwise the initial websocket thread will continue running and utilising memory(and also printing the error messages :D ). Also, don't forget to assign callbacks again.

    How does this defeat the purpose of auto reconnection??
    Auto reconnection just tries to connect with the access_token passed when initializing KiteTicker,to the websocket.
    If access token expires,the only way now is to initialize new KiteTicker instance with valid access token and then connect to websocket.
  • nkotwar
    Yeah, I added ws.stop_retry() inside on_close, and reassigned callbacks as well.
    Thank you :smile:
This discussion has been closed.