Python web socket connection is very unstable, specifically in morning

Anoop
Hi,
I m getting below error very often during morning hours, otherwise it works ok..

Error: Connection error: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)

Code:

def on_connect(ws, response):
ws.subscribe(instrument_list)
ws.set_mode(ws.MODE_FULL, instrument_list)

def on_ticks(ws, ticks):
global live_data
live_data = ticks[:]


kws = KiteTicker(kite.api_key, kite.access_token)

kws.on_connect = on_connect
kws.on_ticks = on_ticks

kws.connect(threaded=True)


  • rakeshr
    Error: Connection error: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
    Go through the websocket FAQs here.
  • Anoop
    Thanks for your quick response Rakesh, I had gone through the FAQs before starting this discussion. There is nothing wrong with the code as it works most of the times. The problem is it fails randomly during morning hours. Sometime it keeps failings for 20 min but then works. I am looking for stability here.

    One point I noticed where you suggested to assign kws.on_ticks = on_ticks in my while loop where I have my trading logic. This, I am not able to understand why? The function name is already assigned in kws instance before starting the while loop correctly. Please advise.
  • Anoop
    hi, any update on above
  • sujith
    We don't see any issue at our end.
  • tahseen
    tahseen edited March 2023
    @Anoop
    I mentioned at other place also. You code might have an issue and when it terminates what you get is the web socket error, which makes you believe into the fact that there is web socket error

    Remember this error simply means that code exited for some issue, that would be in your code. And it also resulted in web socket disconnection uncleanly.

    Please check your code. I can tell you this is not web socket related issue.
Sign In or Register to comment.