WebSocket Disconnects - How to Reconnect

I noticed that during any trading day, my websocket connection will disconnect a few times and will trigger my on_close websocket function (I’m using websockets python library).

Because these intermittent disconnections trigger this ‘on_close’ function, I believe I can easily create a ‘reconnect’ sequence to get the bot back on track automatically.

However, when I manually shut off my internet while the bot was running (to simulate network disconnections on my end), the script simply stops functioning - the ‘on_close’ function doesn’t trigger, so my script doesn’t know when to reconnect.

Anyone have any experience reconnecting a WebSocket when the network that fails is yours?

I put the connect call inside a while loop, and run it as a daemon thread.

from threading import Thread
def crun():
    while True:
Thread(target=crun, daemon=True).start()

Hi @Juan_Rousselot ,
We have an object called StreamConn in the python SDK that does the websocket reconnection for you.
if you are using python, I recommend using that object.
There are plenty of examples on how to use it.

I’m finalizing a profitable (on paper trading) prototype of a bot that I wrote from scratch, and looking at that SDK makes me nauseous because I just picked up python and I’m more of a functional programmer as opposed to object oriented (I’m using multi-threading in my application as every incoming trade could trigger certain actions - wondering if this is a huge “NO!”)

If anyone has a manual way of reconnecting without the SDK, I’d appreciate a look at the code. In the meantime, I will study that SDK and probably look into asyncio. Thanks.