Reconnect feature in python client

Pushwinder
I have this code to just get the LTP value, how to use the reconnect feature in case the connection is down for few minutes, kindly help

def just_data():
i=[]
j=0
kws = WebSocket(api_key, public_token, user_id)
def on_connect(ws):
ws.subscribe([53431303])
ws.set_mode(ws.MODE_LTP, [53431303])
def on_tick(ticks,ws):
for tick in ticks:
j=tick['last_price']
i.append(float(j))
ws.close()
kws.on_tick=on_tick
kws.on_connect=on_connect
kws.connect()
return i[0]
  • sujith
    Hi @Pushwinder,
    When the connection is off websocket error or disconnect is not called immediately. It may sometimes take more than a minute to trigger events. Our approach is based on the basic behavior of websockets. When the connection is on it will receive a heartbeat tick. Kite Connect client expects heartbeat tick every second. If it doesn't get tick even after user specified time interval then it will attempt to reconnect.
    We have added reconnection feature to pykiteconnect. Check out this thread to know how to integrate it.
  • Pushwinder
    Dear @sujith I tried kws.enable_reconnect with new version of kiteconnect, it is giving error
    AttributeError: 'WebSocket' object has no attribute 'enable_reconnect'
  • sujith
    @Pushwinder,
    Example for reconnect looks like this
    kws.enable_reconnect(reconnect_interval=5, reconnect_tries=50)
  • Vivek
    @Pushwinder I guess you are still using the current production Kiteconnect but auto reconnection feature is in feature branch. You can do this

    1. Uninstall current kiteconnect package or create a new virtualenv to test. (pip unininstall kiteconnect)
    2. Install the new package from Github branch -
    pip install https://github.com/rainmattertech/pykiteconnect/archive/master.zip
  • Pushwinder
    Yeah, i tried the same, but enable_reconnect is not showing in its list of attributes,
    Does "kws.reconnect" serve the same purpose???
    Default values are
    reconnect_interval=5,
    reconnect_tries=300
  • sujith
    Hi @Pushwinder,
    Yes. This method call will set a time interval, after which pykiteconnect has to try reconnection and number of times it can retry.
  • Pushwinder
    got the
    kws.enable_reconnect(reconnect_interval=5, reconnect_tries=50)

    had to copy the entire __init__ from https://github.com/rainmattertech/pykiteconnect/blob/reconnection/kiteconnect/__init__.py

    Thanks for your support
This discussion has been closed.