Interruption of WebSocket connection

SAURABH
Hi!
I am facing the following problem with the websocket connection. The websocket connection for streaming the data works smoothly, but after some time, it gets interrupted and disconnects. This bring my whole program to an immediate halt. I have checked my internet connection, which is working fine. What might be causing this problem? And is there a way to fix such a problem?
  • Kailash
    Break in long standing TCP connections over the internet could be caused by numerous issues. Every such client should build in some sort of an auto-reconnect / recovery mechanism. If you use our Python Kite Connect client's built in WS class, it has automatic re-connection.
  • SAURABH
    I am using your built in WS class, but I could not get the command for re-connection. Can you please guide me a bit for the same?
  • SAURABH
    Ok got it, thank you for your help
  • SAURABH
    I tried it, but I am still facing the same problem. I placed the reconnect method of WS class in the on_close event, but still it is of no use. Why is this happening?
  • Kailash
    @SAURABH you needn't call reconnect manually. The class itself should do the reconnection.
  • garpitg
    @Kailash, can you please explain what do you mean by "The class itself should do the reconnection." As per my understanding it should be like this. I hope @SAURABH has done this only ?
    def on_close(ws):
    ws.reconnect()
    Please correct it if it is wrong ?
  • Kailash
    @garpitg KiteConnect in turn uses another library websocket-client, which reconnects automatically. If that isn't working for some reason, the above should help.
  • virtualKG
    @Kailash - I dont think the reconnection is working exactly as planned (although I think it works sometimes - when exactly it works and when it doesn't needs more research). Have done the manual reconnect suggested by @garpitg - so on_error and on_close can handle reconnection issues.

    However, as times, there is a deadlock I think - where the connection is still alive but none of the callbacks are triggered - so no data is being received nor can I reconnect. But my program doesn't terminate as waits for the data. Also the variables

    READ_TIMEOUT = 5
    RECONNECT_INTERVAL = 5
    RECONNECT_TRIES = 300

    in the _init_.py file is defined is initialised but is not passed to the websocket as a parameter (hence my observation that it is not working as expected as I think the default values are being used by the websocket-client).

    So the question is really - once .connect() is called a infinite loop is triggered so can't poll to check whether connection is alive and neither are any of the callbacks being triggered - so cant check for connectivity from there either. So how do I deal with a situation where connection from my end seems active but is not receiving any data ?
  • virtualKG
    virtualKG edited August 2016
    @Kailash - For an example of the above problem - please see screenshot here

    Here - program is still running expecting further quotes at time 13:05 PM... However, the last quote it actually received was on 12:50 PM as seen in the file explorer... So how do I know if data is still being received if no on_error or on_close callback is triggered...
  • Vivek
    @virtualKG We send one byte heartbeat message every second when socket is connected. So you can make use of this to detect any disconnection at your side. Currently on_tick ignores heartbeat data and sends only parsed ticks but you can implement another callback called on_message which gives you heart beat data and raw ticks binary data if there is ticks.
  • virtualKG
    @vivek - Tried using the on_message callback previously as well but even that does not get triggered in the above scenario faced by me.

    Is there a connection reset or timeout that occurs from server end because I think it happens repeatedly at a particular time (say 2 hours after initial connection) - could you please take a look ?
Sign In or Register to comment.