Fixing connection not open issue on js

Hi team, I am using infura websocket with web3 js. I notice that I am getting connection not open error continuously when subscribing to events.

Below here is my event code .

web3js = new web3(new web3.providers.WebsocketProvider("wss://ropsten.infura.io/ws/v3/<token>"));

var contract = new web3js.eth.Contract(contractABI,contractAddress);



 contract.events.successfultransaction({
	fromBlock: "latest",
	toBlock: "latest"
	}, function(error,event){
 		console.log(error);
  		if(!error){
   		pool.getConnection(function(error,connection){
			  console.log(error);
      		if(!error)
       		{
       			var updatequery = "update student_record set transactionstatus='confirmed' where transactionid='"+event.transactionHash+"'";
       			connection.query(updatequery,function(error,result){
	                 connection.release();
 					  console.log(error);
             				if(!error)
                					console.log("Record updated");
 					});

 		}

     	});

   
   
 	}

 });

Can anyone suggest me any fix for this ?

Hey @Mohammad_Obaid, in the Provider connection logic try putting in extra error handling as described in this post here: Handle web socket disconnects · Issue #1354 · web3/web3.js · GitHub . Is the first console.log(error) the one that always errors? Do you see any successful connections?

Also, I would recommend this library by the Status Embark team called Subspace for watching and reacting to state updates: https://subspace.embarklabs.io/ .