WebsocketProvider 'error', 'end', 'close' do not fire on disconnect
Expected behavior
Client application needs to know: a) when it potentially starts losing blocks i.e. when the network goes down/socket disconnects b) when a reconnect is attempted
What are the events that are supposed to fire on a) and b) above?
Where is the documentation describing available WebsocketProvider events? Also, where is the documentation describing WebsocketProvider options such as keepalive, auto, onTimeout etc?
I would expect at least one of 'end', 'error' or 'close' to fire but they do not with web3 1.3.4.
Only 'connect' fires AFTER successful reconnect is made, but this is not enough.
Actual behavior
The only even that fires in the process of losing and reestablishing connection is 'connect' (and also 'error' but only after max reconnect attempts reached
Steps to reproduce the behavior
Run the code below, disconnect wifi after first block, reconnect wifi, wait for another block then disconnect wifi
const Web3 = require('web3');
let endpoint = 'wss://mainnet.infura.io/ws/v3/<...>' ;
let options = {
timeout: 10000, // ms
clientConfig:
{
keepalive: true,
keepaliveInterval: 20000 // ms
},
reconnect: {
auto: true,
delay: 10000, // ms
maxAttempts: 3,
onTimeout: false
}
};
var ep = new Web3.providers.WebsocketProvider(endpoint, options);
ep.on('connect', function ()
{
console.log('! provider connected'); // <- fires after successful connection
});
ep.on('error', function (err)
{
console.log("~ on-error:", err); // <- never fires
});
ep.on('end', async (err) =>
{
console.log("~ on-end:", err); // <- never fires
});
ep.on('close', (event) =>
{
console.log("~ on-close:", event); // <- never fires
});
var web3;
connect();
async function newBlock(error, blockHeader)
{
console.log("block:", blockHeader.number);
};
async function connect()
{
console.log("--> connecting..");
try
{
web3 = await new Web3(ep);
//block part
blockSubscr = await web3.eth.subscribe('newBlockHeaders', newBlock);
console.log("+ subscribe blocks");
}
catch(err)
{
console.log('|* connection error:', err);
}
}
Logs
$ node trywsrecon3.js
--> connecting..
web3-shh package will be deprecated in version 1.3.5 and will no longer be supported.
web3-bzz package will be deprecated in version 1.3.5 and will no longer be supported.
+ subscribe blocks
! provider connected
block: 11947407
-
! provider connected
block: 11947411
~ on-error: Error: Maximum number of reconnect attempts reached!
Environment
web3.js 1.3.4 node 15.10.0 Mac OS X 10.15.7
We'll take a look at this thanks for the detailed report.
We'll take a look at this thanks for the detailed report.
Thank you! Please let me know if you need any additional info. This websocket provider reconnects is an amazing feature that seems to be almost complete - I agree it can't be fully stateful and client will indeed lose block sometimes, but as long as the client can keep clear tabs on what's going on it would be a much much easier/cleaner task, especially with different providers e.g. infura and alchemy and now bsc and others taking different approaches to handling idle clients etc.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.
what's up with this? I can confirm the issue still exists. 😢
still exists in "web3": "^1.8.1"
still exists @spacesailor24
It happens to me when subscribing to a BSC node (binance smart chain), after some time (random time) there is no more data coming from the node, as if it was disconnected but no event fire.
UPDATE : I found a work around :
reconnect: {
auto: true,
delay: 5000, // ms
maxAttempts: 5,
onTimeout: false
}
The issue is that after some time, it reached the maxAttempts.
Related to this issue is :
- Returning empty blocks on new block header sub (fixed by Nethereum) : https://github.com/bnb-chain/bsc/issues/913
- They have issues with their websocket closing connections : https://github.com/bnb-chain/bsc/issues/1105
I tried also to tinker with web3-providers-ws to get some data out of the events but counld't get any. Let me know if you need steps to reproduce.
Need to investigate and reproduce
Hi! I think the issue reported by OP is a client rather than a server one. Regarding the issue: https://github.com/bnb-chain/bsc/issues/1105, this is currently under investigation, but I think those two are totally unrelated. Nevertheless, I will post an update here after we will have a resolution on the bsc issue.
For reconnect event in 1.x you can use this code:
websocketProvider.on('reconnect', function () {
console.log('EVENT reconnect');
});
Here is an example of listening events from was provider (web3js - 1.x)
As I see connect and reconnect events work fine. Also, I've caught the max attempt error in
websocketProvider.on('error', function (err) {
console.log('EVENT error', err);
});
after max reconnection attempts are reached provider does not try to reconnect. So it works as expected
closing this issue as @avkos was not able to reproduce, reopen if this issue is still persisting.
Web3.js 4.x has been released so its highly encouraged to use latest web3.js. https://docs.web3js.org/ https://docs.web3js.org/guides/web3_upgrade_guide/x/ https://www.npmjs.com/package/web3/v/4.0.1 https://github.com/web3/web3.js/releases/tag/v4.0.1