19.1 C
New York
Wednesday, September 23, 2020
Home Future Technology Blockchain How We Handle Blockchain Reconnects In web3js

How We Handle Blockchain Reconnects In web3js

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.


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 
                // removing all the listeners of provider.
                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
                }, reconnectInterval); 
        if (web3Instance == undefined) web3Instance = new web3(provider); 
        else web3Instance.setProvider(provider); 
        return web3Instance; 
module.exports = { 

File app.js

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


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.


Most Popular

A Miami SEO Firm That Can Boost Your Rankings

Miami is a highly competitive market in eCommerce and if you want your website to get seen in the search results, you...

Marijuana SEO is a Priority if You Want to Rank Your Shop

While it’s easier than ever to create an online store to sell any number of different products, actually growing your business and...

Magento Web Development Services For Your Online Shop

Magento is an amazing eCommerce platform that can allow store owners to get their products out onto the Web and make great...

What Makes BigCommerce SEO so Necessary?

No matter what industry you are in or what kind of sales volume you deal with, you need the right search engine...