Handle web socket disconnects
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);
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()
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)
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?
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()
});
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!
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
I am running into the exact same issue. Did anyone have a fix for this?
i'm on parity and i've been getting disconnections too
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.
Anyone have a solution to this?
Having the same issue. Connected to Infura Rinkeby websocket endpoint and it disconnects within minutes.
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?
Same issue here. Surely there's a way to reconnect on error?
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 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
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 This is exactly what I needed. Can't believe I haven't been able to find this code all this time. Thank You!
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.
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
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')
}