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():
try:
await asyncio.sleep(5) # Wait 5 seconds before attempting to reconnect
await instance.connect()
logging.info(“Reconnected to websocket”)
break
except Exception as e:
logging.error(f"Reconnection attempt failed: {e}")
continue

@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

@Saurbah_Vadukia
Can you add debug and see whats going on?

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)

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