I'm developing an application on a private Ethereum chain and trying to listen to some events. I wrote a basic indexer script for listening to events and writing them in a database.

To listen to the events I am connecting to the provider via Web3.providers.WebsocketsProvider. I realized it keeps losing the connection every minute. And actually it does not disconnect, instead, it creates a new connection every minute as I don't catch any logs from error or end events.

As a workaround, I added reconnect options introduced in web3.js v1.2.7. That's how I noticed the regular dropping each minute.

Here's the code:

const Web3 = require('web3');
const TruffleContract = require('@truffle/contract');
const ReviewStorageArtifact = require('./contracts/ReviewStorage.json');
const { logAddedReview } = require('./reviewLogger'); // external module where I write to database.

const providerLink = 'wss://providerLink.org';

// Enable auto reconnection
const options = {
  reconnect: {
    auto: true,
    delay: 1000, // ms
    maxAttempts: 5,
    onTimeout: false

// Connect
let provider, web3;
provider = new Web3.providers.WebsocketProvider(providerLink, options);
web3 = new Web3(provider);

provider.on('connect', init);
provider.on('error', e => console.log('WS Error', e));
provider.on('end', e => {
  console.log('WS closed');

// Connect to instance and listen event
function init() {
  const ReviewStorage = TruffleContract(ReviewStorageArtifact);
  console.log('Trying to conntect weeb3');
    .then(instance => {
      console.log('Found instance');
      instance.ReviewAdded() // Listen to ReviewAdded events.
        .on('data', (event) => logAddedReview(event, instance))
        .on('error', console.error);

Here's the output how I noticed the regular reconnections. Notice there are no outputs from .on('error') or .on('end'): Output of websocket reconnection

Why could this be happening? Might it be something on the provider side? When I connect to Infura Rinkeby, for example, there are no reconnections taking place. Thanks.

1 Answer 1


Try this options

const options = {
    timeout: 30000, // ms

    clientConfig: {
        // Useful if requests are large
        maxReceivedFrameSize: 100000000,   // bytes - default: 1MiB
        maxReceivedMessageSize: 100000000, // bytes - default: 8MiB

        // Useful to keep a connection alive
        keepalive: true,
        keepaliveInterval: -1 // ms

    // Enable auto reconnection
    reconnect: {
        auto: true,
        delay: 1000, // ms
        maxAttempts: 10,
        onTimeout: false

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.