Last active
January 28, 2022 01:02
-
-
Save maurodelazeri/73dc1e5d009d5282b348314cc6335518 to your computer and use it in GitHub Desktop.
auto reconnect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Web3 = require("web3"); | |
| const newProvider = () => new Web3.providers.WebsocketProvider('XXXXXXXXXXXXXXXX', { | |
| reconnect: { | |
| auto: true, | |
| delay: 5000, // ms | |
| maxAttempts: 5, | |
| onTimeout: false, | |
| }, | |
| }) | |
| const web3 = new Web3(newProvider()) | |
| const checkActive = () => { | |
| if (!web3.currentProvider.connected) { | |
| web3.setProvider(newProvider()) | |
| } | |
| } | |
| setInterval(checkActive, 2000) | |
| const subscription = web3.eth.subscribe("pendingTransactions", (err, res) => { | |
| if (err) console.error(err); | |
| }); | |
| var init = function () { | |
| subscription.on("data", () => { | |
| setTimeout(async () => { | |
| try { | |
| web3.eth | |
| .getPastLogs({ | |
| fromBlock: "latest", | |
| toBlock: "latest", | |
| }) | |
| .then((logs) => { | |
| console.log(logs.length); | |
| }); | |
| } catch (err) { | |
| console.error(err); | |
| } | |
| }); | |
| }); | |
| }; | |
| init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment