6

I am using web3 version 1.0 version 26, however I can't receive any events from ganache-cli and testrpc.

  this.Contract.events.Created({},{ fromBlock: 0, toBlock: 'latest' }, function(error, event){ console.log(event); })
                        .on('data', function(event){
                              console.log(event);
                        })

this is my provider connection

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

is httpprovider not working for the watch events? do I have to use ws provider?

3
  • The HttpProvider should work fine (and does, in my experience). Can you share the rest of the code (e g. where you define this.Contract and where you generate the events)?
    – user19510
    Jan 1, 2018 at 11:00
  • i have another question posted here, ethereum.stackexchange.com/questions/31747/…, you can check this.
    – user824624
    Jan 1, 2018 at 11:02
  • Sorry, I haven't used version 1.0.0 yet, and I was wrong. You do indeed need to use the WebsocketProvider. See my answer.
    – user19510
    Jan 1, 2018 at 11:23

3 Answers 3

11

You're correct that the HttpProvider doesn't support subscribing to events in web3.js 1.0.0. From https://web3js.readthedocs.io/en/1.0/web3.html#value:

Object - HttpProvider: The HTTP provider is deprecated, as it won’t work for subscriptions.

You will indeed need to use the WebsocketProvider (or IpcProvider for local nodes).

3
  • can ganache-cli or testrpc support ws right now or any other ways to start a testnet supporting ws
    – user824624
    Jan 1, 2018 at 11:25
  • No, as far as I can tell, ganache doesn't support websocket connections yet. See, e.g., github.com/trufflesuite/ganache-cli/issues/369.
    – user19510
    Jan 1, 2018 at 11:29
  • But yes, geth supports it, so you should be able to spin up a test network that way. (I've never done this myself.) Or you could use an existing test network like Ropsten or Rinkeby.
    – user19510
    Jan 1, 2018 at 11:30
5

I can confirm websocket connections are supported by Ganache. I used

  web3 = new Web3()
  const eventProvider = new Web3.providers.WebsocketProvider('ws://localhost:7545')
  web3.setProvider(eventProvider)
2
  • How did you do this in the truffle console? It won't let me import Web3
    – Andrew
    Feb 5, 2020 at 18:07
  • Do it in javascript code Jun 24, 2020 at 16:43
3

Subscriptions are currently also supported through the websocket API of Infura:

const RINKEBY_WSS = "wss://rinkeby.infura.io/ws";
var provider = new Web3.providers.WebsocketProvider(RINKEBY_WSS);
var web3 = new Web3(provider);
var name = 'newBlockHeaders'; // e.g.

web3.eth.subscribe(name, (error, result) => {
        if(!error) {
            console.log(result);
        } else {
            console.log('Error:', error);          
        }
    }).on("data", function (transaction) {
        console.log(transaction);
});

See here for the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.