Also should i use web3 = new Web3(provider) or web3.setProvider(provider) to reinit?
In general case when you have to connect to Ethereum network, it is recommended that you use the getDefaultProvider()
method because it will connect to both Etherscan and INFURA simultaneously.
For example.... If your use case is to connect to an existing Web3 Provider
// When using a Web3 provider, the network will be automatically detected
// e.g. HTTP provider
let currentProvider = new web3.providers.HttpProvider('http://localhost:8545');
let web3Provider = new ethers.providers.Web3Provider(currentProvider);
Is there a way to listen for web3 connection to node continuously and
restart if it fails.
I don't know if there are guideline in Ehthereum for this, but I think you should implement some logic to manage this scenario, keeping in mind this:
WebSockets provide a persistent connection between a client and server that both parties can use to start sending data at any time. The client establishes a WebSocket connection through a process known as the WebSocket handshake. This process starts with the client sending a regular HTTP request to the server.
You are sure the client, so you will have way to play/intercept with Events in order to manage Connection Error.
Take a look at CloseEvent in the docs of WebSocket and search or try to write something with Web3.
A CloseEvent is sent to clients using WebSockets when the connection is closed. This is delivered to the listener indicated by the WebSocket object's onclose attribute.