WebSocketProvider handle ws close and reconnect
See original GitHub issueHi @ricmoo,
I’m using WebSocketProvider server-side to listen to blockchain events and performing calls to smart contracts. Sometimes the websocket pipe got broken and I need to reconnect it.
I use this code to detect ws close and reconnect but it would be nice to not have to rely on _websocket
to do it:
let wsProvider;
init = async () => {
wsProvider = new ethers.providers.WebSocketProvider(wsHost);
wsProvider._websocket.on('close', async (code) => {
console.log('ws closed', code);
wsProvider._websocket.terminate();
await sleep(3000); // wait before reconnect
init();
});
wsProvider.on('block', doStuff);
};
I also noticed when the websocket is broken Promise call don’t reject wich is not super intuitive.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:24
- Comments:52 (4 by maintainers)
Top Results From Across the Web
web3js - Websocket provider reconnects every 1 minute
As a workaround, I added reconnect options introduced in web3.js v1.2.7. That's how I noticed the regular dropping each minute. Here's the code:...
Read more >Using the reconnect option with web3.js WSS connection
Introduction When your WSS connection closes abnormally, you may get the following errors: code: 1006,reason: 'Socket Error: read...
Read more >What is use-case of error: "Provider started to reconnect ...
In web3-provider-ws node_module's code I find this reconnect function: WebsocketProvider.prototype.reconnect = function () { var _this ...
Read more >Providers — Web3.py 5.31.3 documentation
If you must connect to a node on a different computer, use Websockets. ... This provider handles interactions with an WS or WSS...
Read more >[SOLVED] CONNECTION ERROR with web3 with websockets ...
I´m using moralis socket url to connect web3 and HDWalletProvider. ... Error: CONNECTION ERROR: The connection got closed with the close code 4040...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think this is probably the best solution:
This send a ping every 15 seconds, when it sends a ping, it expects a pong back within 7.5 seconds otherwise it closes the connection and calls the main
startConnection
function to start everything over.Where it says
// TODO: handle contract listeners setup + indexing
that’s where you should do any indexing or listening for contract events etc.Fine tune these timing vars to taste, depending on who your Node provider is, this are the settings I use for QuikNode
To elaborate on @mikevercoelen’s answer, I extracted the logic to a function
Then in my code, i get: