1

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

  • Since you mention inconsistency, I take it upon myself to guess that you possibly forget to close the WS connection in some cases, thus failing to open it thereafter. Unlike HTTP connections, WS connections should be closed explicitly. – goodvibration Apr 16 at 13:24
  • Isn't it managed by the web3js library itself? – klautern Apr 16 at 13:25
  • Not for WS connections. You can do this with 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 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.