-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle web socket disconnects #1354
Comments
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
|
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,..
|
Anyone have a solution to this? |
Having the same issue. Connected to Infura Rinkeby websocket endpoint and it disconnects within minutes. |
Same issue here.
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:
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 Edit: In the options for the WebsocketProvider I'm using |
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 ***@***.***> 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
<#1354 (comment)>,
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.
|
@mkhraisha I haven't been able to test this, but I think I had a similar issue that I fixed using |
@dmihal trying to connect to |
are there any updates on a fix for this issue? |
This works for me
|
@anhnt , I tried your solution. It's just shows the |
@dmihal , As per your solution if we use Ws instead of Wss, it will not work with HTTPS.
so sorry! This solution is not working. |
@cmaliwal are you using it on browser. My solution works great on nodejs but havent tested it on browser just yet, I think there is issue with ssl certificate not web3 |
Thank you. I was afraid of that. :/ I am using web3@1.0.0-beta.55 . Should I be switching to 2.0? |
This issue should be reopened. Watching the provider for IPC, or WebSocket does work on either 2.0.0-alpha or beta.55 @melnikaite. Furthermore, intentionally crashing a geth node also does not trigger these events. At the moment it seems impossible to detect any kind of websocket close. The only event that works is 'connect'. 'error' and 'end' are not detected at all. Sample: const Web3 = require('web3')
const net = require('net')
const web3 = new Web3('/path/to/geth.ipc', net)
const provider = web3.currentProvider
// works
provider.on('connect', () => {
console.log('connected')
})
// doesn't work
provider.on('error', e => {
console.error('error', e)
})
// doesn't work
provider.on('end', e => {
console.error('end', e)
})
// EDIT: THIS WORKS use the close event instead
provider.on('close', e => {
console.error('close', e)
}) EDIT: I couldn't find any information about this in the documentation, so I inspected the prototype of the provider and it emits a |
You are correct, only connect works. @melnikaite is correct as well it seems. Even if you reconnect, the subscription/getPastEvents no longer listens for incoming events. You have to nuke the whole script, which is not a workaround for production. |
There are different reasons for disconnect. When I shutdown my local node manually the provider does get an |
For future devs looking for a solution to keeping a WebSocket connection to Infura alive, this worked for me const provider = new Web3.providers.WebsocketProvider(
config.PROVIDER_WS,
{
// @ts-ignore
clientConfig: {
keepalive: true,
keepaliveInterval: 60000
}
}
);
The |
I should add |
Thanks for your solution @naddison36, seems to resolve the auto idle disconnect problem for me. Otherwise, it seems it does not prevent other disconnect events. To resolve them and thus ensure a connection stability, I'm personally adding reconnect: {
auto: true,
delay: 1000,
maxAttempts: 10,
}, which is set to |
Thanks @joZephhh. I've added the reconnect config to my listener service. I was trapping the close event on the provider and crashing my process as per the following. Since my process was running on Kubernetes, it would be restarted but your approach is much better. provider.on("close", err => {
logger.error(`WebSocket connection closed. Error code ${err.code}, reason "${err.reason}"`);
process.exit(1);
}); |
@joZephhh and @naddison36 Thank you so much... I am really grateful |
I looking for workaround how to handle this error within my app. It was a surprise why websocket not store pending request to resolve after reconnection. /app/node_modules/web3-core-helpers/lib/errors.js:63 |
also getting this one..did you manage to find a solution? |
@sloambbr Unfortunately, not yet |
Would you mind opening a new issue if you believe there to be one. Thank you! |
For future reference, there are some extra parameters required for full auto-reconnect websocket provider -
|
What if it gets end second time? @anhnt |
@joZephhh does this fix all disconnects or do we still have to listen on end events and create new provider? |
As far as I remember it resolved all my disconnects issues! 💪 |
This does in fact help reconnect to the websocket provider, however if your code relies on a constant connection to the rpc node and some specific setup of listening to a contract address or wallet, then aside from the reconnecting the provider, you would also need to update your contract or wallet object. For those still encountering problems, it could be deeper that just the reconnect configuration option. Since you configured the provider to restart automatically on drop, emit a "reconnect" event to catch a connection drop
and then in your code where you used the provider instance to instantiate your contract object:
This way, when the provider reconnects automatically, you can reattach the listeners of your contracts. Assuming you have a listener to a contract/address for some specific "transfer" event, you would recall your function to setup the listener again with the new provider. Without doing so, your contract object will keep listening to events from the old dropped provider. In my code, im resetting the listener when the connection drops:
Hope this helps! |
@georgiod9 Thanks, this solved my problem. I had the same issue, even tho i handled the reconnect issue, my code coudn't catch the new events that was emitted after the reconnection. If your contract is listening to events, reconnecting to the websocket provider alone is not enough. You need to reattach the event listeners
|
Im making an app that monitors a specific addresses' balance, im using Infura's public websocket endpoint and web3@1.0
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);
The text was updated successfully, but these errors were encountered: