19 C
London
Tuesday, July 7, 2020

How We Handle Blockchain Reconnects In web3js

- Advertisement -

In this article, we’ll learn how to handle blockchain disconnects in the production environment automatically, using Web3JS. The method described below works for Web3JS version 1.0.0-beta.35, but it should be good for the stable 1.2.* versions as well.

Problem Description

If your team uses Web3JS in production, then you must be aware that there’s no inbuilt reconnect functionality in Web3JS to handle blockchain disconnects or restarts. So, usually, when there is a connection drop, NodeJS service needs to be restarted as well to connect to the blockchain again. Not a very practical approach.

Solution

Let’s see how we can gracefully handle blockchain disconnects in NodeJS. In the Web3JS library, the Provider object gives us events for

  1. Connect — connection established
  2. Error — provider errors
  3. End — provider connection ended.

Upon disconnection, we can utilize the end event to reinitiate a new Web3JS connection. Let’s look at an example to understand this:

File connection.js

In this file, we’ll handle the connection between NodeJS and blockchain. We will have a newBlockchainconnection method which will return a Web3 active connection object.

const web3 = require("web3");
let hasProviderEnded = false, web3Instance, reconnectInterval = 10000; 
async function newBlockchainConnection(webSocketProvider, endCallback) { 
        // create new provider
        const provider = new web3.providers.WebsocketProvider(webSocketProvider); 
        hasProviderEnded = false;
        // connect event fires when the connection established successfully.
        provider.on('connect', () => console.log("connected to blockchain"));
       
        // error event fires whenever there is an error response from blockchain and this event also has an error object and message property of error gives us the specific reason for the error
        provider.on('error', (err) => console.log(err.message));
 
        // end event fires whenever the connection end is detected. So Whenever this event fires we will try to reconnect to blockchain
        provider.on('end', async (err) => { 
                // handle multiple event calls sent by Web3JS library 
                if (hasProviderEnded) return; 
  
                // setting hashProviderEnded to true as sometimes the end event is fired multiple times by the provider
                hasProviderEnded = true; 
 
                // reset the current provider 
                provider.reset(); 
                // removing all the listeners of provider.
                provider.removeAllListeners("connect"); 
                provider.removeAllListeners("error"); 
                provider.removeAllListeners("end"); 
  
                setTimeout(() => { 
                         // emitting the restart event after some time to allow blockchain to complete startup
                         // we are listening to this event in the other file and this callback will initialize a new connection
                          endCallback();
                }, reconnectInterval); 
        }); 
        if (web3Instance == undefined) web3Instance = new web3(provider); 
        else web3Instance.setProvider(provider); 
        return web3Instance; 
} 
  
module.exports = { 
         newBlockchainConnection 
}

File app.js

const connection = require("connection");
const web3JSConnection; 
const endCallback  = async function () { 
        web3JSConnection = await connection.newBlockchainConnection('ws://127.0.0.1:8545', customEvent); 
 }); 
  
 async function getWeb3Connection() { 
         if (web3JSConnection == undefined) web3JSConnection = await connection.newBlockchainConnection('ws://127.0.0.1:8545', endCallback); 
     return web3JSConnection; 
 } 
 
module.exports = { 
        getWeb3Connection 
}

Summary

On blockchain disconnection, when the provider triggers the ‘end’ event, we are triggering a callback after a timeout. The event then, in turn, calls the function to create a new blockchain connection.Some points to note:

  1. Sometimes Web3JS sends you multiple ‘end’ events for the same connection drop, so we have to check if we have already handled the disconnection event once
  2. Setting the new provider in the web3 instance object using ‘setProvider’ instead of creating a new web3 instance
  3. Resetting the provider and removing active listeners
  4. Reconnect interval must be at least 5 seconds as usually, it takes about 5 seconds for the blockchain to restart.

Many technology-first companies like Skeps are developing a revolutionary product using blockchain. Stay tuned till our next write-up on this new-age technology.

- Advertisement -
SourceSwati

Latest news

Top 5 Features That Help You Build a Successful Real Estate Auction Portal

Real estate auctions are now becoming an effective way to get a good deal on investments. It is popular among both deep-pocketed...

Download JIOFi Latest Version Firmware FXN_JMR540_R6.16

JIOFI 3 firmware update file download Do you want to download JIOFI Latets Version Firmware.

Top Renowned New Android 11 Features

if you are one of the top developers, your wait for the latest Android 11 is over! The Android app studio has...

Sports Betting Tips: How Sports Betting Got Started?

What is the easiest way of cashing out huge amounts in the shortest amount of time? The answer is betting. Betting provides...

Related news

Top 5 Features That Help You Build a Successful Real Estate Auction Portal

Real estate auctions are now becoming an effective way to get a good deal on investments. It is popular among both deep-pocketed...

Download JIOFi Latest Version Firmware FXN_JMR540_R6.16

JIOFI 3 firmware update file download Do you want to download JIOFI Latets Version Firmware.

Top Renowned New Android 11 Features

if you are one of the top developers, your wait for the latest Android 11 is over! The Android app studio has...

Sports Betting Tips: How Sports Betting Got Started?

What is the easiest way of cashing out huge amounts in the shortest amount of time? The answer is betting. Betting provides...
- Advertisement -