use case :on_close() method

RP3436
Can someone explain if the on_close method is for close of connection at server end or to handle something similar at client end? What is the use of this method/ how this method should be used?
  • trade_then
    I am not sure of python client but I think nomenclature should be the same for all clients libs from Zerodha. So assuming it has something to do with websocket. then it simply means that a connection from your app to kite server which was established via websocket is closed.

    it is invoked at 2 locations
    1. while connecting to kite server and the connection could not be established.
    2. when gracefully closing the connection. when connection is no longer required

    whatever might be it. but your previous websocket connection can no longer be used for communication. this is all happening at client end when connection to server is lost. server is not sending OnClose to you. it is your kite client lib detecting it and announcing to you.

    At which point you can check if your app requires this connection then it can try to restart it depending on the logic of your application. or clear resources.

    Thanks
    Regards
  • RP3436
    @trade_then Thanks for the nice explanation. Second case is quite straight forward when the disconnection is intended or by choice. But in case 1- if the connection is not established then where is the question of closing it? Should it not be "disconnection of the already established connection" for reasons other then those designed by the user (i.e. not graceful)? Supposing that is the case or even otherwise, if one needs to reconnect in such eventuality , under which method should the relevant code be placed(under on_close??). Would be great if you could throw some more light on this.
  • trade_then
    trade_then edited February 2018
    I am sorry I was giving a general view based on .net c# client.

    in 1st case usually it is because of error. OnError is announced first before announcing OnClose. Onclose is only announced if connection is ("Forbidden") && Contains("403") so that means it did reach the server and then came back with the reply. so perhaps that is why it is announced there. but before it OnError is announced.

    As far as coding goes. I don't think you can do much when
    1. connection was never established and you are receiving forbidden in the error described in OnError.

    In this case you might have to check your client credentials. assess token etc. or perhaps inquire from Zerodha why you are being denied connection. It might be because of rate limiting that you already have open websocket connections and opening a new connection might exceed the limit of connections which are allowed.

    2. there is a feature of reconnection which Zerodha client libraries handle themselves. that is if a connection is lost they try to reconnect it anyway. So instead of trying to establish the connection yourself you might increase the _retryCount while initiating your Identifier to establishing the connection, for the Client lib to know how many times it should retry before giving up. because these retries would also announce OnClose() of previous connections when they detect information is not flowing.

    because of 2. I don't think you should attempt Re-connection when you receive Onclose but when you receive OnNoReconnect(). which is being announced by the zerodha client libraries that they tried to establish connection as mentioned in _retryCount but have failed to do so. now you can decide what to do.

    Thanks
    Regards
  • RP3436
    Thanks a lot . You took lot of pain to explain so much in detail. Now the concept is clear.
Sign In or Register to comment.