SourceSwati

How We Handle Blockchain Reconnects In web3js

Top Tech Firms

Top 10 Sports Betting App and Sports Mobile App Development Companies

Sports betting is one of the oldest forms of gambling, carried out by generations for a very...

Top Mobile App Development Companies in USA and Worldwide

The global headcount of app developers is growing exponentially. Paradoxically, the gap between mobile and web app...

Exploring the Top 10 Blockchain Companies From Australia

Recently, the Australian government published a 52-page report on its vision regarding blockchain development in the country. Essentially, the...
James Hook
An experienced Content Writer to work with a Big 4 consultancy on an exciting programme in the technology/AI (artificial intelligence) field, specifically within the transportation sector. Key responsibilities for the Content Writer include: Create white papers discussing subject matter in the technology/AI field, for applications within the transportation sector Utilise existing content ensuring it meets brand guidelines and drives the strategic priorities of the organisation Work collaboratively with colleagues The Successful Applicant will ideally have: Ability to produce written content, including editing and proofreading Strong understanding of technology language, drivers and outcomes Understanding of MS Office applications, Adobe Acrobat, Photoshop etc. Unrivalled attention to detail Good organisational skills including the ability to manage and reconcile competing priorities Good communication and interpersonal skills Ability to interact with stakeholders at various levels and ensure objectives are met Self-motivated, flexible and proactive attitude Exceptional English language skills

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 -

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Future Technology

All You Need to Know About the Life-Changing Digital Twin Technology

“Digital twin is a digital representation of a physical entity that helps in tracking and modifying the activities...

WhatsApp Users Hit 2 Billion: What Does This Mean for the Future of Privacy?

There are now over 2 billion registered users on the mobile messaging platform, up from 1.5 billion in 2017. Brief History...

How We Made a Simple Avatar Generator for Our Fitness Interviews

My name is Mads Phikamphon and I'm the founder of Bulk Hackers. At Bulk Hackers, we interview people who do great...

The Importance of Unlearning Emerging Technologies

The world of software is constantly changing at a very fast pace. Yesterday’s axioms might be tomorrow’s anti-patterns. Newborn technologies rise to popularity only to become...

How AI Could Save the 3D Printing Industry and the Future of Machines

3D printing is a billion-dollar market with a variety of use cases- from healthcare, replicas to architecture, airplane parts.

More Articles Like This

- Advertisement -