Introduction
When your WSS connection closes abnormally, you may get the following errors:
code: 1006,
reason: 'Socket Error: read ECONNRESET'
When using the WSS connection to your node with web3.js, it is recommended to use the reconnect option to make sure the connection is reattempted.
See the web3.js documentation on reconnect.
How-to
Example to subscribe to receiving the latest block number with web3.js and the reconnect option:
const Web3 = require('web3');
var options = {
reconnect: {
auto: true,
delay: 5000, // ms
maxAttempts: 5,
onTimeout: false
}
};
const web3 = new Web3(new Web3.providers.WebsocketProvider('WSS_ENDPOINT', options));
web3.eth.subscribe('newBlockHeaders', function (err, result) {
if(!err) {
console.log(result.number);
}
});
where WSS_ENDPOINT is your node WSS endpoint. See View node access and credentials.
See also Handle real-time data using WebSocket with JavaScript and Python.
Comments
0 comments
Please sign in to leave a comment.