web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

Handle web socket disconnects

Open mikedpid opened this issue 8 years ago • 69 comments

Im making an app that monitors a specific addresses' balance, im using Infura's public websocket endpoint and [email protected] I often get disconnected and get "Connection not open on send()" I tried using a set interval that polls web3.eth.net.isListening() that resets the web3 provider, making it to reconnect to the node but in some cases i get Unhandled rejection Error: CONNECTION ERROR: Couldn't connect to node on IPC. and the app crashes. How am i supposed to handle the disconnects and make my app reconnect to the node?

setInterval(function() { web3.eth.net.isListening() .then() .catch(e => { console.log('[ - ] Lost connection to the node, reconnecting'); web3.setProvider(config.infura_ws); app(); // this just subscribes to newBlockHeaders event }) }, interval);

mikedpid avatar Feb 04 '18 11:02 mikedpid

Still looking for help, im checking for an address' balance every 5seconds and after a couple minutes i keep getting connection not open on send()

mikedpid avatar Feb 14 '18 17:02 mikedpid

I have this problem as well although, i am not subscribing on a setInterval, my workaround is catching the event promise error and when it happens, i just reconnect it again with a new Web3(infura_ws_url) replacing the old one

Not sure if the problem is from web3 or infura (ws)

7Ds7 avatar Feb 15 '18 14:02 7Ds7

I believe its the infura ws node thats crashing, i tried a try catch block but the app still crashes sometimes. Im trying to subscribe to new pending txs but its unstable af and after a couple seconds i get loads of promise errors and more than half of the transactions are not being checked. Do i fix this issue if i host myself a node that will be accessed only by my app?

mikedpid avatar Feb 15 '18 14:02 mikedpid

Using my own geth instance for testing purposes i did not get this error, although you might try not having it run on a setInterval, you can check the subscribed event error, i am doing something along these lines

function listenToEvent() {}
    contract.events.SomeEvent({
          toBlock: 'latest'
        }, (error, event) => { console.log(event); })
          .on('data', (event) => {
            console.log('--contract.events.SomeEvent--');
          })
          .on('changed', (event) => {
           console.log('--SomeEvent--Changed');
         })
          .on('error', (e) => {
            console.log('--SomeEvent--Error');
            console.log(e);
           your_web3_instance = new Web3('wss://infura_ws_url') });
           wait_for_your_web3_instance_to_be_connected
           listenToEvent()
      });

7Ds7 avatar Feb 15 '18 14:02 7Ds7

Im subscribing to the new pending txs and checking each transaction’s “to” and “value” fields, its for a payment system but as i said, using infura and it crashes after a couple seconds. Will try using my own parity node to see if the connection still crashes, will let you know!

mikedpid avatar Feb 15 '18 14:02 mikedpid

same issue here.. even if I subscribe to new blocks/subscribe to events and poll a contract calling an immulable method periodically, sending a transaction/calling a payable method would often result in immediate Connection not open on send() error

jamboj avatar Mar 03 '18 17:03 jamboj

I am running into the exact same issue. Did anyone have a fix for this?

Plinpod avatar Mar 11 '18 22:03 Plinpod

i'm on parity and i've been getting disconnections too

BransonGitomeh avatar Mar 16 '18 12:03 BransonGitomeh

this is also happening trying to extend web3 1.0 to handle debug_traceTransaction over websocket. wsapi is set to admin,debug,..

web3.extend({
  property: 'debug',
  methods: [{
    name: 'traceTransaction',
    call: 'debug_traceTransaction',
    params: 1
  }]
])
await web3.debugIt.traceTransaction('0xe09418975ecca6dfb43aa9ee4ac64597f84b4dcc920e6872d2fb9fd592671f35')
Error: CONNECTION ERROR: Couldn't connect to node on IPC.

cryptophonic avatar Mar 31 '18 20:03 cryptophonic

Anyone have a solution to this?

Plinpod avatar Apr 18 '18 02:04 Plinpod

Having the same issue. Connected to Infura Rinkeby websocket endpoint and it disconnects within minutes.

kingjerod avatar Apr 18 '18 06:04 kingjerod

Same issue here.

connection not open on send() Unhandled rejection Error: connection not open

I'm using public websocket endpoint to check the latest block height 'web3.eth.getBlock("latest") ' every 5 seconds.

Does Infura's api have any limitations?

damozhang avatar Apr 23 '18 05:04 damozhang

Same issue here. Surely there's a way to reconnect on error?

elie222 avatar Apr 26 '18 14:04 elie222

Resetting the provider upon disconnect semi works:

web3.setProvider(NODE_URL)

The only problem is if that if you were listening to events you need to start listening again.

elie222 avatar Apr 26 '18 14:04 elie222

@elie222 how to know disconnection? if there was an event released on disconnection then we could use that to restart listening, but i cant find that in docs

BransonGitomeh avatar Apr 27 '18 07:04 BransonGitomeh

You can listen to events from the WebSocketProvider to detect disconnects.

  const provider = new Web3.providers.WebsocketProvider(path);
  provider.on('error', e => console.error('WS Error', e));
  provider.on('end', e => console.error('WS End', e));

dmihal avatar Apr 27 '18 14:04 dmihal

@dmihal This is exactly what I needed. Can't believe I haven't been able to find this code all this time. Thank You!

Plinpod avatar Apr 27 '18 19:04 Plinpod

I have noticed even when running my own private geth instance that the websocket will disconnect after almost exactly 1 minute, even with reconnects. The only way around this is to send a request like the previously mentioned web3.eth.net.isListening() call.

Edit: In the options for the WebsocketProvider I'm using {timeout: 30000}. Not sure if that somehow corresponds to 1 minute, but I did try upping it to 120000 just to see what happens. Still disconnects after ~1 minute.

kingjerod avatar Apr 27 '18 22:04 kingjerod

i used to get disconnections with parity running on a really small server, i dont get it as often now with a larger ram capacity

On Sat, Apr 28, 2018 at 1:53 AM, Jerod King [email protected] wrote:

I have noticed even when running my own private geth instance that the websocket will disconnect after almost exactly 1 minute, even with reconnects. The only way around this is to send a request like the previously mentioned web3.eth.net.isListening() call.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ethereum/web3.js/issues/1354#issuecomment-385113543, or mute the thread https://github.com/notifications/unsubscribe-auth/AKqeD-IaZ6U-glekTox_wuoq6GntwJaCks5ts6FNgaJpZM4R4h01 .

-- Much Regards

Branson G Kuria Systems Developer Sabek Systems

P.O Box 78024 Nairobi.

www.sabek.co.ke +254 711 657108

BransonGitomeh avatar Apr 28 '18 09:04 BransonGitomeh

I tried using a snippet like the following but i could never get it to reconnect, i keep getting a CONNECTION ERROR: Couldn't connect to node on WS.


const provider = web3.currentProvider;
provider.on("error",e=> handleDisconnects(e));
provider.on("close",e=> handleDisconnects(e));
	
	function handleDisconnects(e) {
		console.log("error",e);
		web3.setProvider('wss://ropsten.infura.io/ws')
		
	}

mkhraisha avatar May 01 '18 18:05 mkhraisha