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);
}
});
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:
Based on the code you provided, it can be changed to the following:
Please note that this feature was made available on Web3.js v1.2.7.
References: