Connection was closed uncleanly error observed by every 2 or 3 days.

KarthikJayaprasad
Hi Kite Team,

First of all, thanks for all your support & coordination.
I am using python client ticker, I have been observing "ERROR:kiteconnect.ticker: Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))" by every 2 or 3 days.

For example, I have generated new "access_token" on day 1 & KiteTicker is working fine with the ticker for 2 or 3 days and sometime in-between observed above error, and force to generate new "access_token".

I am sure, I am closing the connection using the right method. Need your guidance/help, please explain what is the process. Let me know if any more details required.

Following is Code Executed on Linux OS-

#!/usr/bin/env python3
import logging
from kiteconnect import KiteTicker

logging.basicConfig(level=logging.DEBUG)

# Initialise
kws = KiteTicker(api_key, access_token)

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

def on_connect(ws, response):
# Callback on successful connect.
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
try:
print("Starting The Connection")
ws.subscribe([53520391])
print("Connected Sucessfully")

# Set RELIANCE to tick in `full` mode.
#ws.set_mode(ws.MODE_FULL, [53520391])
except Exception:
print(Exception)

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

def close(self, code=None, reason=None):
print("'Close' the WebSocket connection.")
if self.ws:
self.ws.sendClose(code, reason)

# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.close = close
kws.on_close = on_close

# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
kws.connect()
  • rakeshr
    @KarthikJayaprasad
    Access token are flushed every day at 7:30 AM and new one is generated every day at 7:35AM.So, make sure you generate access token post 7:35 AM on the trading day.
  • amallik
    Access token is generated but i am getting error "Connection closed: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: name 'logger' is not defined)" while connecting to ticket. Can someone help me to identify the issue ?
    It was working till yesterday . Is there any limit generating the access_token because I ran the script twice. not sure if that caused the issue ?
  • rakeshr
    @amallik
    WebSocket TCP connection: name 'logger' is not defined
    Above error states the cause of issue,'logger' variable is not defined.
  • amallik
    amallik edited November 27
    @rakeshr ,removed the logger stament but still getting error "Connection closed: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: close reason without close code)"
  • rakeshr
    @amallik
    Can you remove ws.stop() and check ? If still facing issue paste your ticker code here.
  • KarthikJayaprasad
    @rakeshr
    Thanks for your response, but I need clarifications on the below points, please.

    Your Previous Comments: "Access token are flushed every day at 7:30 AM and new one is generated every day at 7:35 AM.So, make sure you generate access token post 7:35 AM on the trading day."

    Clarification 1: If I generate the access token once, I can use the same token for x days...? [i.e. I don't need to generate access token every day after 7:35 AM] Please confirm.
    Clarification 2: In maximum case, for me, the access token is active till 2+ days & then it throws "(403 - Forbidden)" error, I would like to why/RCA, please.
    Clarification 3: Is there a way to generate a request token programmatically, if need to generate the access token every day...?

    Please provide your valuable input, thanks in advance.
  • sujith
    An access token is flushed every day. It is valid only for one trading day.

    We don't kill Kite Ticker connections when access tokens are flushed. Authentication is done only while connecting. Your connection might be getting closed only after two to three days probably.

    It is mandatory by the exchange that a user has to manually login at least once a day. We don't recommend automating login.
  • KarthikJayaprasad
    Thanks, @sujith , For your clarification.
This discussion has been closed.