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

bhargavramts
I Recreated the entire app on zerodha api portal and passing just sample one instrument token but still getting same error. I replaced the api key and access token multiple times but still getting same error.


import logging
from kiteconnect import KiteTicker

# Configure logging for debugging
logging.basicConfig(level=logging.DEBUG)

# Replace with your actual API key and access token
api_key = "xxxxxxc68wbxxxxx"
access_token = "xxxxxxxxxxewCS5D60ecCQxxxxxxxxxxx"

# Initialize KiteTicker
kws = KiteTicker(api_key, access_token)

# Callback function to receive ticks
def on_ticks(ws, ticks):
logging.debug(f"Ticks: {ticks}")
# You can process the received tick data here

# Callback function on successful connection
def on_connect(ws, response):
logging.info("WebSocket connected successfully.")
# Subscribe to a list of instrument_tokens (e.g., RELIANCE and ACC)
# Replace with your desired instrument tokens
instrument_tokens_to_subscribe = [738561, 5633] # Example tokens
ws.subscribe(instrument_tokens_to_subscribe)

# Set the mode for specific instruments (e.g., RELIANCE in full mode)
ws.set_mode(ws.MODE_FULL, [738561]) # Example: set RELIANCE to full mode

# Callback function on connection close
def on_close(ws, code, reason):
logging.info(f"Connection closed: {code} - {reason}")
# Stop the main loop if you don't want automatic reconnection
# If you want reconnection, do not call ws.stop() here
ws.stop()

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

# Connect to the WebSocket
# This will run an infinite loop on the main thread,
# so subsequent code will only execute after the connection is closed.
kws.connect()

logging.info("WebSocket connection terminated.")
Tagged:
Sign In or Register to comment.