ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly

mld
Hi Team,

I am new to Python client for the Kite Connect trading API. I am using Ubuntu 14.04 LTS.

Installed Python client using pip install kiteconnect --upgrade --pre

pip show kiteconnect showing kiteconnect version 3.7.0b5

Today I ran the the official example (from github) of how to run KiteTicker in threaded mode through terminal.
For the first run it produced desired output. But I terminated the program using Ctrl + C.
After that whenever I am trying to run the official example, I am getting the following error:

INFO:root:This is main thread. Will change webosocket mode every 5 seconds.
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
INFO:root:Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
INFO:root:Connection closed: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))

How to correct it and what is the correct way of closing the websocket connection cleanly?


  • sujith
    You need a valid access token to connect to Kite Ticker. It seems like you are not passing a valid api key or access token.
  • tanigai
    facing similar problem
    ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))


    even though i gave valid api key and access token
  • sujith
    @tanigai,
    The new Kite Ticker expects valid credentials for connecting to the ticker. If you see 403 then it means either api_key is invalid or access token is invalid.
  • rvsw
    Hello

    I'm having a similar problem. I'm using the code that is provided at this link:https://kite.trade/docs/pykiteconnect/v3/

    I'm using the code to read the prices with the live streaming during market hours. While the order placement is possible, I'm unable to read the prices.

    One of the differences I see between the two codes is that for live streaming, there is no place to provide the API secret. With the kite ticker, one can provide the API key as well is the session but there is no place to provide the API secret.

    I'm using the version 3 and the date today is June 28, 2018
  • TUSH123
    same issue I am facing after upgrading to Kiteconnect latest api version

    ticker:Connection error: 1006 - connection was closed uncleanly
  • rakeshr
    @TUSH123
    Can you go through this thread?
    Make sure, there is no assignment related error on_tick method.
  • Kartikey
    I am getting same error , How to resolve this . I am using valid API key and Token
  • PJ_2018
    I am facing this same issue since last 2 days in a row.
    The connection is closed with websocket error 1006.
    Yesterday it closed at 1:56pm and today I got this error at 2:06pm.

    I am running my code on Google cloud so internet connectivity or any other issue with the setup is out of question. The connection is closed from the zerodha server.
  • rakeshr
    rakeshr edited October 2019
    @PJ_2018
    We don't close the Websocket streaming at our end.
    Websocket streaming connection will be closed if you are blocking the on_tick method by performing any calculation/activity inside.So, make sure that you offload all calculation to another thread from on_tick thread. You may check the threading example here.
  • PJ_2018
    PJ_2018 edited October 2019
    @rakeshr ,

    There is absolutely no calculations/activities inside on_ticks as I am just fetching the ticks and have not coded any strategy yet. I was just testing my intial code set-up on Google cloud platform.

    I have one simple question though on the below piece of code for on_close.
    def on_close(ws, code, reason):
    print("Websocket Error Code: ", code)
    print("Reason: ", reason)
    # ws.close()
    # ws.stop()
    kws.on_close = on_close

    I have read in the documentation that "Auto reonnection is enabled by default and it can be disabled by passing reconnect param while initialising KiteTicker."

    Although, the ws.close and ws.stop are commented out in the above code but if they are used inside on_close, would it stop websocket from trying to reconnect.

    Also, please let me know how to override the default parameters for RECONNECT_MAX_TRIES, RECONNECT_MAX_DELAY and CONNECT_TIMEOUT.

    Thanks.
  • rakeshr
    @PJ_2018
    Although, the ws.close and ws.stop are commented out in the above code but if they are used inside on_close, would it stop websocket from trying to reconnect.
    Yeah, ws.stop() will terminate Websocket connection along with error message. So, if you are looking to re-connect, even in case of error,you need to remove ws.stop().
    Also, please let me know how to override the default parameters for RECONNECT_MAX_TRIES, RECONNECT_MAX_DELAY and CONNECT_TIMEOUT.
    You can override those params while initializing Websocket connection, check it here.
  • PJ_2018
    PJ_2018 edited December 2019
    Thanks for your help. Will let you know if I receive the error again.
  • yen_786
    Can anyone please help me on this error ?

    ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))

    I have read the entire thread and I am passing a valid api_key and recent request token.
    I am using the below code to connect to kiteticker

    kws = KiteTicker(api_key,request_token)
    tokens = [260105,256265] #token numbers of instruments

    def on_ticks(ws, ticks):
    logging.debug("Ticks: {}".format(ticks))
    print(ticks)

    def on_connect(ws, response):
    # Subscribe to a list of instrument_tokens
    ws.subscribe([260105,256265])
    ws.set_mode(ws.MODE_FULL,[260105])

    def on_close(ws, code, reason):
    # On connection close stop the main loop
    # Reconnection will not happen after executing `ws.stop()`
    ws.stop()

    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close
    kws.connect()

  • sujith
    It seems like your session is expired, you may have to create a new access token.
  • yen_786
    @sujith Resolved, thanks for your help.
  • igotmail9
    igotmail9 edited January 23

    sorry found spelling mistake
This discussion has been closed.