I have implemented the following code in python for consuming websocket : ##### from kiteconnect import KiteTicker kws = KiteTicker(api_key, access_token)
def on_connect(ws, response): # Callback on successful connect. # Subscribe to a list of instrument_tokens (RELIANCE and ACC here). ws.subscribe(tick_stock) # Set RELIANCE to tick in `full` mode. ws.set_mode(ws.MODE_FULL, tick_stock) time.sleep(30) ws.unsubscribe(tick_stock) kws.close()
def on_close(ws, code, reason): # On connection close stop the main loop # Reconnection will not happen after executing `ws.stop()` print("Closing ws connection: ", code, reason) ws.stop()
You might find this similar discussion here helpful for understanding how to stop the WebSocket connection. You could consider tweaking it by using a sleep function to close the connection after a few seconds.