Starting WebSocket... ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time)) Error: 1006 connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time)). Getting this error since yesterday. The access token and API key are fine, I am able to make calls to get quote successfully. However the streaming api fails with the error message above. I am running with a minimal client to rule out any client specific issues. Here is the code I am working with.
from kiteconnect import KiteConnect, KiteTicker import logging import vault.secrets as secrets
logging.basicConfig(level=logging.DEBUG)
# Create a KiteTicker instance kws = KiteTicker(api_key=secrets.API_KEY, access_token=secrets.ACCESS_TOKEN)
Looks like your firewall might be blocking or limiting the WebSocket connection. You may check your firewall settings, change the network and check. You may refer to the similar discussion here.
This was working for months and stopped working yesterday. Dont think I have changed anything in my network settings. Besides, I am seeing the issue with WIFI, Mobile Hotspot, the office internet (It was working with all three till yesterday). I tried debugging with chatgpt. It suggested using websocket directly... import ssl import logging import websocket from vault.secrets import API_KEY, ACCESS_TOKEN
# Run WebSocket with SSL context in run_forever ws_app.run_forever(sslopt={"ssl_context": ssl_context}) Running this, the process gets stuck not printing anything. ChatGPT suggests this is the handshake issue...
We have checked and verified the WebSocket connection on our end and did not observe any issues. To troubleshoot further, please follow these steps:
1. Review the WebSocket example code here and check if it produces any issues. You can also refer to the Python WebSocket streaming FAQs here. 2. Verify if a firewall might be blocking or limiting the WebSocket connection. 3. Test the connection using an alternate ISP to rule out any network-related issues. 4. This usually happens when ws takes longer to complete a handshake, i.e WebSocket handshake timeout field, mainly due to heavy load. You need to inspect the callback ticker instance.
This was an Airtel issue. They had made some changes which caused this issue when using broadband. In case anyone else sees the issue, perhaps call 121 and get support. It likely requires some change in the "backend".
import logging
import websocket
from vault.secrets import API_KEY, ACCESS_TOKEN
logging.basicConfig(level=logging.DEBUG)
# Kite WebSocket URL
ws_url = f"wss://ws.kite.trade/connect?api_key={API_KEY}&access_token={ACCESS_TOKEN}"
# Create TLS 1.2 SSL context
ssl_context = ssl.create_default_context()
ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2
# Callbacks
def on_message(ws, message):
print("Received message:", message)
def on_error(ws, error):
print("Error:", error)
def on_close(ws, close_status_code, close_msg):
print("Closed:", close_status_code, close_msg)
def on_open(ws):
print("Opened connection")
# Example: Subscribe to NIFTY 50 instrument token
ws.send('{"a":["subscribe",[256265]]}')
# Create WebSocketApp
ws_app = websocket.WebSocketApp(
ws_url,
on_message=on_message,
on_error=on_error,
on_close=on_close,
on_open=on_open
)
# Run WebSocket with SSL context in run_forever
ws_app.run_forever(sslopt={"ssl_context": ssl_context})
Running this, the process gets stuck not printing anything. ChatGPT suggests this is the handshake issue...
Can you check if there is any throttling or any other restriction on my app key?
Symptoms:
WebSocket connections fail immediately with error 1006 / opening handshake timeout.
HTTP API calls (LTP, orders, margins, etc.) are working fine.
Minimal Python test scripts also hang indefinitely during handshake.
Environment / Diagnostics:
Python 3.11.6, OpenSSL 3.4.1 (MacOS)
kiteconnect 4.x, websocket-client latest version
TLS 1.2 enforced in client SSL context
Tested on multiple networks (home WiFi, mobile hotspot, office)
No lingering Python processes holding the app key (lsof -i :443 shows nothing)
TCP connectivity verified:
openssl s_client -connect ws.kite.trade:443 -servername ws.kite.trade
→ CONNECTED
Minimal WebSocket test using Python hangs without printing any ticks / messages.
Steps already tried:
Updated Python packages (kiteconnect, websocket-client).
Enforced TLS1.2 explicitly in SSL context.
Attempted multiple networks to rule out ISP / routing issues.
Verified no old WebSocket sessions are active.
Issue persists:
Connection hangs during the handshake.
No error message is printed after starting the connection.
Could you please check if:
There is any throttling / stale sessions associated with my app key.
Any server-side WebSocket node or regional routing issues could be causing this.
There is a recommended workaround until the server-side issue is resolved.
1. Review the WebSocket example code here and check if it produces any issues. You can also refer to the Python WebSocket streaming FAQs here.
2. Verify if a firewall might be blocking or limiting the WebSocket connection.
3. Test the connection using an alternate ISP to rule out any network-related issues.
4. This usually happens when ws takes longer to complete a handshake, i.e WebSocket handshake timeout field, mainly due to heavy load. You need to inspect the callback ticker instance.