I know this question have been asked several times and many solutions are being proposed as a workaround but not sure why it is not been working on our case.

In our case we have to listen for contract events like Transfer events on our backend (node js) so that we can update our investor balances and perform other sensitive activities. We can't afford to lose events in our case.

For this, we are using WebSocket provider from infura and web3js library to connect with them. We noticed that if for a few hours we are'nt make any calls then it would close that connection and we would have to restart our app for new connections. To work around this issue we applied popular community workaround i.e listen for events like end or error on the provider and if those events occur we would make a call to instantiate a connection but after applying this patch web3 throws below error instead of connection not open error

'Connection dropped by remote peer.

Here is simplified code of our blockchain module.

const web3  = require('web3');
const ethtx = require('ethereumjs-tx');
const path = require('path');
const fs = require('fs');
const Tx = require('ethereumjs-tx');

const updatebalancedb  = require('../db/Admins/Transactions/updatebalance.js');
const dotenv = require('dotenv');

var masterwalletpublickey = process.env.daopublicaddress;
var masterwalletprivatekey = Buffer.from(process.env.daoprivatekey, 'hex');
var wsscocketprovider = process.env.infuranode;
var parentcontractaddress = process.env.maincontractaddress;

var provider = new web3.providers.WebsocketProvider(wsscocketprovider);

var web3js = new web3(provider);

provider.on('error', e => 
console.log('WS Error', e);
provider = new web3.providers.WebsocketProvider(wsscocketprovider);
provider.on('end', e => {
    console.log('WS closed');
    console.log('Attempting to reconnect...');
    provider = new web3.providers.WebsocketProvider(wsscocketprovider);

    provider.on('connect', function () {
        console.log('WSS Reconnected');


 const rawcontract  = JSON.parse(fs.readFileSync(path.join(__dirname,'../BlockchainModule/build/contracts/ErContract.json')));
 const contractabi = JSON.stringify(rawcontract.abi);

const contract = new web3js.eth.Contract(rawcontract.abi,parentcontractaddress);

.on('data', (event) => {
    var data = event;
.on('error', console.error);

const gettotaltokens = function(){

    return   contract.methods.totaltokens().call()
            return result;

           throw error; 


const circulationtokens = function(){

    return contract.methods.circulationsupply().call()
            return result;
            throw error;


When we call functions like gettotaltokens after few hours of connectivity web3 throws such error .

Can anyone give some pointers or some guide of how to solve this messy issue?

The only solution I have in my mind is to move web3 provider code to a functional level and every time we call the function it will start a fresh new connection. That will solve the problem for time being but not very scalable and also wouldn't solve event listener issues.

Tech Environment:

  1. Node js (v10.13.0)
  2. web3.js (1.2.3)
  3. Chain : (Ropsten)
  4. Socket Provider (Infura)

3 Answers 3


I hope someone from Infura would comment the specifics when they terminate a connection.

However, the latest Web3.js has autoreconnect feature:


It should alleviate your problems.


I am also facing this issue on a project I am working on where it is critical to not have any missed events. What I decided to do was to store the block height of the last event in-memory, and then use an event filter to fetch any events that were emitted after the connection was dropped and before the connection was re-established.

Note that I'm using Python and Web3.py with websocket_client


class ws():
  def __init__(contract_address):
    self.w3 = Web3(Web3.WebsocketProvider(config.WS_RPC_URL))
    self.contract_address = contract_address
    self.block_height = None

  def start(self):
    self.conn = websocket.WebSocketApp(config.WS_RPC_URL, on_open=self.on_open, on_message=self.on_message, on_close=self.on_close, on_error=self.on_error)

        while True:

  def on_open(self, conn):
        "id": 1,
        "method": "eth_subscribe",

    current_block = self.w3.eth.get_block_number()

    event_filter = self.w3.eth.filter({
      'address': [self.contract_address],
      'topics': [self.event_signature_hash],
      'fromBlock': self.block_height,
      'toBlock': current_block

    missed_events = event_filter.get_all_entries()

  def on_message(self, conn, message):
    self.block_height = ['params']['result']['blockNumber']

I think the best solution is to make KeepAlive (ping pong) to avoid drop from WS RPC provider,

The sample is in Etheres js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.