npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

multi-http-provider

v0.1.3

Published

Enable the web3 api to connect to several Ethereum nodes to ensure that a node will be reached if others are down.

Downloads

9

Readme

Documentation page for multi-http-provider

Package that sits on top of web3 package by frozeman. This package adds a MultiHttpProvider in the Web3 module: This provider enables to connect to 1, 2 or more geth nodes simultaneously using the underlying HttpProvider of the web3 module. The provider has the following behavior

  • It is initialized with an array of hosts, and establish connection with the first available.
  • When an http request fails to reach the host (ie the geth node) the connection is considered dead and the connection is moved to the next available one. The onChange callback is raised. The failed request is sent again and result returned to the caller transparently.
  • Any filter subscriptions active are resubscribed with the new node (transparently) and any callback will continue to work.

You need to run one or several local Ethereum node(s) to use this library.

Installation

Node.js

npm install multi-http-provider --save

Meteor.js

meteor npm install multi-http-provider --save 

Usage

Loading the packages. Second require will modify Web3.providers

var Web3 = require('web3');
require('multi-http-provider'); // adds the MultiHttpProvider to the Web3.providers

Initializing and use Web3 API normally. Also see web3 package documentation

let ethereum_url1 = "http://localhost:8545"; // node one
let ethereum_url2 = "http://localhost:8546"; // node two
web3 = new Web3(new Web3.providers.MultiHttpProvider([ethereum_url1, ethereum_url2], <optional timeout>));
console.log("Connected to Geth console", web3.version.node, "on block", eth.blockNumber);
eth.defaultAccount = eth.coinbase;
web3.personal.unlockAccount(eth.defaultAccount, "capture your password here", 10);

output: Connected to Geth console Geth/v1.5.8-stable-f58fb322/darwin/go1.7.5 on block 62353

Register a onChange callback:

  • param 1 (multi): is the instance of the MultiHttpProvider
  • param 2 (http) : is the instance of the HttpProvider currently connected with. If no node is available, this param is null.
var onConnectionChange = function(multi, http){
			console.log("MultiHttpProvider changed to:",(http?http.host:"no connection"));
			}
web3.currentProvider.onChange(onConnectionChange);

Documentation of internal attributes

Only the most relevant

hosts

the array of hosts provided in construction. You can eventually modify this array dynamically.

currentProvider

the current HttpProvider in use. will be "undefined" if no connection is available

switchToNextConnected()

Function called automatically when the provider detects a connection issue with the current HttpProvider. You can call this function yourself if you wish to switch manually (load balancing for instance) When called, this function will (in that order)

  • select the next available HttpProvider (meaning where a rpc call can be made)
  • recreate all subscriptions (in synchronous calls) and store the new filterId (see below)
  • call the onChange callbacks that have been registered.

subscriptions

An object dictionary with the filterId registered (as available in the Filter object of the web3 module returned by the various filter and event functions) it contains in front of each filterId a structure as follow:

  • newId: the filterId in the current geth node (possibly different than the one the filter has been register into first)
  • method: the geth rpc method used to create the filter
  • params: the parameters provided to the node to create the filter. see json rpc documentation of geth node for filter at https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newfilter
> web3.currentProvider.subscriptions
{ 
   '0x4e1e5457d84ef49a0ba97d972329dfd9': 
            { newId: '0x3a05fc1f64e0fd75d780a8319f01ba88',
              method: 'eth_newBlockFilter',
			params: [] } 
}

This structure allows to recreate the subscription to the new geth node when the connection switches and to map the original filterId (known by web3 module) with the current filterId known in the current geth node. DO NOT TAMPER THIS STRUCTURE UNLESS YOU KNOW WHAT YOU ARE DOING.

_traceMessage

set to true to activate the log on the console of the outbound and inbound messages as communicated to/from this provided

<-- { json message returned to web3 }
--> { json message received from web3 }

Change log

v 0.1.2

  • correction of a bug that did not properly released the subscriptions
  • addition of the internal field _traceMessage (default is false) to log on the console what the provider send and receive