0

I'm trying to subscribe to the ethereum blockchain using a managed AWS ethereum node. I followed the tutorial here: https://docs.aws.amazon.com/managed-blockchain/latest/ethereum-dev/ethereum-json-rpc.html (The section titled To make an Ethereum API call using web3.js over WebSockets to your Ethereum node on Managed Blockchain)

It all worked fine, so I launched the process on pm2, but then I came back later only to find the websocket connection failed with this error message: Error: CONNECTION ERROR: The connection got closed with the close code '1001' and the following reason string 'Going away'

What is the best strategy for handing this? AWS was kind enough to provide a helper script (see below), but I'm thinking that I need to change this code to handle websocket disconnects. I'm fairly new to both Node and websockets so I would appreciate any pointers!

/////////////////////////////////////////////////////
// Authored by Carl Youngblood
// Senior Blockchain Solutions Architect, AWS
// Adapted from web3 npm package v1.3.0
// licensed under GNU Lesser General Public License
// https://github.com/ethereum/web3.js
/////////////////////////////////////////////////////

import AWS from 'aws-sdk';
import WebsocketProvider  from 'web3-providers-ws';
import pkg from 'websocket';
const { w3cwebsocket } = pkg;
const Ws = w3cwebsocket;

export default class AWSWebsocketProvider extends WebsocketProvider {
  connect() {
    const region = process.env.AWS_DEFAULT_REGION || 'us-east-1';
    const credentials = new AWS.EnvironmentCredentials('AWS');
    const host = new URL(this.url).hostname;
    const endpoint = new AWS.Endpoint(`https://${host}/`);
    const req = new AWS.HttpRequest(endpoint, region);
    req.method = 'GET';
    req.body = '';
    req.headers['host'] = host;
    const signer = new AWS.Signers.V4(req, 'managedblockchain');
    signer.addAuthorization(credentials, new Date());
    const headers = {
      'Authorization': req.headers['Authorization'],
      'X-Amz-Date': req.headers['X-Amz-Date'],
      ...this.headers
    }
    this.connection = new Ws(this.url, this.protocol, undefined, headers, this.requestOptions, this.clientConfig);
    this._addSocketListeners();
  }
}

Here is my code which calls this and subscribes to the blockchain

import Web3 from 'web3';
import AWSWebsocketProvider from "./aws-websocket-provider.js";
...
const web3 = new Web3(new AWSWebsocketProvider(endpoint));
var subscription = web3.eth.subscribe('newBlockHeaders', function(error, result) {
  if (error) {
    console.log(error);
  }
  else {
    analyze(result);
  }
});
Nate L
  • 465
  • 1
  • 5
  • 17

1 Answers1

0

I am a Sr. Blockchain Specialist at AWS and am glad to help you here.

You can pass additional options as the second parameter, specifying the keep-alive and reconnect-on-failure options, on exactly the same way as to how Web3.js handles it. In short:

new AWSWebsocketProvider(endpoint, options)

Based on the code you provided, it can be changed to the following:


import Web3 from 'web3';
import AWSWebsocketProvider from "./aws-websocket-provider.js";

var 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: 60000 // ms
    },

    // Enable auto reconnection
    reconnect: {
        auto: true,
        delay: 5000, // ms
        maxAttempts: 5,
        onTimeout: false
    }
};

const web3 = new Web3(new AWSWebsocketProvider(endpoint, options));

Please note that this feature was made available on Web3.js v1.2.7.

References:

FRAGA
  • 5,078
  • 1
  • 15
  • 13
  • Wow thanks for this! I am going to try it out and then mark your answer as the accepted answer – Nate L Aug 14 '21 at 05:56
  • Seems like it still is closing, although it is no longer throwing that error message. I tried finding documentation on those options, and it kept directing me to this, which is rather sparse: https://github.com/theturtle32/WebSocket-Node/blob/v1.0.31/docs/WebSocketClient.md#client-config-options – Nate L Aug 17 '21 at 06:50
  • yep, still closing for me as well – Jim Dec 05 '21 at 17:49
  • Hi all, the connection is subject to closing, due to a variety of conditions. The snippet provided would also reconnect using WS client. Did the re-connection attempt happen? – FRAGA Apr 13 '22 at 15:18