I have an ethereum web application using web3js 1.0.0-beta.34 on a private network. Most of the times everything works fine. Sometimes the connection to the node through a websocket provider fails with the error:
web3.min.js:1 WebSocket connection to 'wss://myserver.com/quorum/node4/ws' failed:
Error during WebSocket handshake: Unexpected response code: 400
I cannot replicate the error consistently, and my guess is that is related to the network conditions. Something like I start using the web3 library before is connected. This is how it's initilialized:
const provider = new Web3.providers.WebsocketProvider("wss://myserver.com/quorum/node4/ws");
const web3 = new Web3(provider);
const contract = new web3.eth.Contract(contractAbi, contractAddress, { gasPrice: 0, gas: 4500000 });
Is there a better way to initialize web3 so I can ensure the connection has been stablished?
Thanks
if (web3.currentProvider.constructor.name == "WebsocketProvider") web3.currentProvider.connection.close();
. Give it a try and let me know if it works for you. – goodvibration Apr 16 '19 at 13:26