Websocket client handle reconnect

Hey Support team,

Basically I use a websocket client in my web application to connect to Infura. Specifically:

In order to keep the connection alive I started implementing the onerror and onclose handler functions for the websocket to reconnect if that happens. That works great for certain errors, but I noticed that sometimes after a long period of inactivity it seems like the connection is still open and I am able to send messages but I don’t receive a response by infura for like 2 minutes until the onclose handler of the socket I implemented fires and replaces the socket.

However this is much to late for my use cases. What’s the best way to deal with it? Is it good practice to periodically close and replace the connection on my end to make sure there is always a working socket connection open?

Thanks for any suggestions!

Hi @janschmutz, it is good practice to periodically close and reopen the connection, you’re absolutely correct! As you said, this ensures that when you do have those long periods of inactivity, you won’t see side effects like the no-response you’re currently getting.