16

I am using infura for connecting to testnet/mainnet as my server side web3 provider.

This works for most requests and operations, but it does not seem to work for Events.

const allEvents = (event, callback) =>
  event({}, { fromBlock: 0, toBlock: 'latest' }).get((error, results) => {
  if (error) return callback(error);
  results.forEach(result => callback(null, result));
  event().watch(callback);
});

allEvents(contractInstance.Event, eventCallback);

The code above runs locally (connecting to a localhost:8545 provider / geth) but does not run if I use infura:

// THIS DOES NOT WORK:
const web3Url = `https://ropsten.infura.io/${infuraKey}`;
const web3 = new Web3(new Web3.providers.HttpProvider(web3Url));

// THIS WORKS:
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

Any suggestions or alternatives for web3 providers? Thanks.

eth
  • 85,679
  • 53
  • 285
  • 406
carlolm
  • 2,326
  • 1
  • 13
  • 24
  • Having same problem. Did you find a solution for this yet? – Miguel Mota Sep 01 '17 at 17:49
  • For the time being, I've actually set it up so that I use client's web3 (Metamask) to monitor events and send it to the server when detected. This works for my purposes.

    The alternative is setting up a VM on a server/AWS and run a geth node and use that to monitor events. That's probably what I'll do longer term.

    – carlolm Sep 01 '17 at 18:12
  • Thank you so much! This is the only way that worked for me to get all events, out of 5-6 other ways I tried. Also, Infura supports WebSocket now - yay! :) – Svante May 10 '18 at 16:17
  • I am using web3 0.20.6 and using the followin code, but I donot see anything when the event fires, I am using nodejs and including web3. Can any one tell me why the event is not showing anything, i even tried to use watch method, i dont get errors either. var Web3 = require('web3'); web3 =new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/key") ContractInstance.ShippingEvent(function(error,result){ if(error) { console.log("Shipping Event :" + error); } else { console.log("Shipping Event: " + result); console.log("Shipping Event: " + result.args._shipper); console.log("Shipping – Rameez Saleem Jul 14 '18 at 02:02

3 Answers3

14

Infura currently doesn't support WebSockets (required for events using Web3 v1, otherwise you get the error "The current provider doesn't support subscriptions" when using infura as HttpProvider), so what you have to do is run a local geth node that connects and syncs to the network.

Here we enable the websocket flag and allow any origin to connect to the local geth node that is syncing with the rinkeby testnet:

geth --rinkeby --ws --wsport=8546 --wsorigins="*" --datadir=$HOME/.rinkeby --cache=512 --rpc --rpcapi="personal,eth,network" --rpcport=8545 --fast --bootnodes=enode://a24ac7c5484ef4ed0c5eb2d36620ba4e4aa13b8c84684e1b4aab0cebea2ae45cb4d375b77eab56516d34bfbd3c1a833fc51296ff084b770b94fb9028c4d25ccf@52.169.42.101:30303

To check the syncing status:

geth attach ipc:$HOME/.rinkeby/geth.ipc
> eth.syncing

Once it's done syncing you can connect using the WebsocketProvider:

const web3 = new Web3(new Web3.providers.WebsocketProvider('ws://localhost:8546'))

You should now be able to receive events:

myContract.events.allEvents((error, event) => {
  if (error) {
    console.error(error)
    return false
  }

  console.log(event)
})

Tested this with geth v1.6.5 and web3 v1.0.0.

Update Oct 11, 2017:

Infura is experimenting with websocket support. You can ask for beta access in this github issue thread:

https://github.com/INFURA/infura/issues/29

Miguel Mota
  • 5,143
  • 29
  • 47
  • Which of the geth params are required to listen to events on the mainnet? Does geth --ws --wsport=8546 --wsorigins="*" --rpc --rpcapi="personal,eth,network" --rpcport=8545 --fast also work or what use have the diff. params, thx? – Andi Giga Nov 30 '17 at 08:44
  • @AndiGiga For mainnet use network id 1 e.g. --networkid=1 (default). Yes that should work. – Miguel Mota Nov 30 '17 at 17:58
  • @MiguelMota const provider = new Web3.providers.WebsocketProvider('wss://rinkeby.infura.io/ws/v3/560e70f3ebfc4f628a53f4143b9460fd'); const web3= new Web3(provider); This code is not retieving me the accounts const accounts = await web3.eth.getAccounts();. Do you have idea? – Darey Mar 11 '19 at 16:46
  • @Darey infura doesn't provide wallet management so getAccounts() will be an empty array. getAccounts() will work on your local node however. – Miguel Mota Mar 11 '19 at 20:21
  • @MiguelMota But I wanted to deploy contract into rinkeby test network using one of my metamask accounts. I have added my source code here https://ethereum.stackexchange.com/q/68145/51276 where I was able to get the account using http provider. Thanks for the help – Darey Mar 12 '19 at 01:07
12

I have been using Infura with web3 1.0 in mainnet. Here is my code, hope it helps

var Web3 = require('web3')
var request = require('request');
var contract = require('truffle-contract')
var zastrin_pay_artifacts = require('./build/contracts/ZastrinPay.json')
var ws_provider = 'wss://mainnet.infura.io/ws'
var web3 = new Web3(new Web3.providers.WebsocketProvider(ws_provider))
var ZastrinPay = contract(zastrin_pay_artifacts);
var econtract = new web3.eth.Contract(ZastrinPay.abi, '<address>');

console.log("Starting listner ....");

newPaymentEvent = econtract.events.NewPayment({fromBlock: 5424000, address: '<address>', toBlock: 'latest'}, function(error, result){
  if (result !== undefined) {
    var args = result.returnValues;
    args["_txn"] = result.transactionHash;
    console.log(args);
  }
});
maheshmurthy
  • 1,237
  • 2
  • 14
  • 17
  • 2
    Thanks, yes infura added web sockets within the past few months that now allows for connecting to events – carlolm Apr 12 '18 at 05:01
  • Hi @maheshmurthy, my events work but I get the notification: Error: The current provider doesn't support subscriptions: HDWalletProvider.

    What do you think could cause this?

    – gbenroscience Jun 25 '20 at 13:52
1

QuikNode.io works well. Dedicated ETH node supporting both https:// and wss:// (websockets). Supports event subscriptions and txpool/queue API calls; Parity and Geth clients; MainNet/Ropsten/Rinkeby/Kovan too. Can sign up on website and have node running in minutes.

haxsyn
  • 76
  • 3