How to reconnet websocket connection

How I can Reconnect websocket automatically while script is running ?

I tried but it is not working like this,

async def on_close(instance):
logging.warning(“Connection has been closed. Attempting to reconnect…”)
await reconnect(instance)

Function to handle reconnection logic

async def reconnect(instance):
while not shutdown_event.is_set():
await asyncio.sleep(5) # Wait 5 seconds before attempting to reconnect
await instance.connect()“Reconnected to websocket”)
except Exception as e:
logging.error(f"Reconnection attempt failed: {e}")

@Hardik can you help with this ? what it is not reconnecting ?

Hello @Saurbah_Vadukia

Will have to deploy and check this scenario. @Sammy @neo2564 if you can take a look and suggest?

Sure @Hardik
I will deploy the code and update you accordingly

Can you add debug and see whats going on?

import logging

Couldyou please tell me how to reconnect the sockets or disconnect the socket