I’m working on a live market data dashboard using the KiteConnect API. My client provides me with their API credentials, including a daily updated access token. They also run their own trading program on a different machine using the same login details.
The REST API calls work perfectly fine when I fetch market instruments:
from kiteconnect import KiteTicker, KiteConnect
api_secret = 'my_api_key'
token = 'daily_access_token'
connection = KiteConnect(api_key=api_secret, access_token=token)
data = connection.instruments(exchange=connection.EXCHANGE_NFO)
However, I encounter a 1006 error when trying to connect to live streaming data:
The error message indicates: “Connection error: 1006 - connection was closed uncleanly.”
Could using the same API credentials from two different locations simultaneously be the cause of this WebSocket connection issue? Any advice on how to resolve this streaming problem would be greatly appreciated.
Yeah, that’s the issue. Multiple websocket connections with the same credentials will cause drops and 1006 errors. KiteConnect only allows one active websocket session per API key.
I hit this same problem building market data systems. The fix isn’t just connection management - you need a proper data pipeline that handles multiple consumers without auth conflicts.
What worked: set up an automated workflow as a single connection point to KiteConnect, then distribute data to multiple endpoints. One websocket session serves your dashboard and client’s trading program simultaneously.
The workflow captures streaming data, processes it real-time, and routes it to databases, APIs, or multiple apps without connection conflicts. Add automatic reconnection and error handling to make it bulletproof.
This eliminates credential sharing and gives you way better control over data flow and errors than managing multiple direct connections.
Indeed, the 1006 error stems from having multiple concurrent websocket sessions. I faced the same challenge while developing a similar dashboard. KiteConnect restricts each API key to a single websocket connection, meaning your client’s trading program connects and disrupts your own.
A solution that worked for me involved implementing connection pooling on your end. Establish one primary websocket for your data source, then buffer and distribute that market data to various internal consumers. This approach allows you to maintain only one KiteConnect connection while supplying data to your dashboard and other applications.
Also, review your reconnection handling; excessive 1006 errors can result from rapid reconnection attempts. I introduced exponential backoff delays between each attempt, which significantly reduced connection conflicts. If possible, coordinate connection timings with your client to optimize performance.
Indeed, multiple websocket sessions are the source of your 1006 errors. I’ve encountered similar challenges while developing trading infrastructure for clients needing concurrent data feeds across various applications. The root of the problem is that KiteConnect restricts each API key to a single websocket connection. When your client’s trading program initiates a connection, it terminates your existing session, resulting in the unclean disconnect error.
To address this, consider implementing a centralized data service. This approach involves setting up a dedicated service that maintains a single KiteConnect websocket connection, acting as a data broker. This service retrieves the market data and distributes it through Redis streams or message queues to your dashboard and other applications that require the data.
Additionally, ensure to incorporate proper connection state management, including heartbeat monitoring and fallback mechanisms for connection interruptions. This will prevent issues with reconnection loops that can exacerbate the 1006 errors. While you might consider coordinating connection timings with your client, the broker service approach is generally more sustainable and effective in the long run.
had the same issue last month! you’re definitely hitting the concurrent session limit. add some basic logging to see exactly when the disconnect happens - it’ll help you figure out if it’s your code or the api booting you when a new client connects.
Still Kite is allowing 3000 instruments per websocket and what if I need more than 10000 instruments of data to stream into my own platform’s watchlist, when I try to connect to multiple websockets using a single API key sometimes it runs great and sometimes it fails randomly with this message “WebSocket Connection Closed - Reason : connection was closed uncleanly (None) , Code : 1006”
any solution on this, or should I restrict to connecting 1 websocket ?
Still Kite is allowing 3000 instruments per websocket and what if I need more than 10000 instruments of data to stream into my own platform’s watchlist, when I try to connect to multiple websockets using a single API key sometimes it runs great and sometimes it fails randomly with this message “WebSocket Connection Closed - Reason : connection was closed uncleanly (None) , Code : 1006”
any solution on this, or should I restrict to connecting 1 websocket ?