1

I'm using web3 1.0, lets say this is my smartcontract:

contract Foo {
    event NewUser(
        address userAddress,
        uint amount
    );

    function addUser() public payable{
        emit NewUser(msg.sender, msg.value);
    }
}

The events get fired in Remix perfectly well.

The problem is when I run a nodejs with express back-end and I try to watch the events with the following piece of code:

contract.events.NewUser({}, function(error, event){ 
  console.log(event); 
})
.on('data', function(event){
  console.log(event); // same results as the optional callback above
})
.on('changed', function(event){
  // remove event from local database
})
.on('error', console.error);

The first log gets called with value null and when the event can't get triggered. Does anyone have any idea what could be the issue?

5
  • 1
    What provider are you using? Make sure your provider supports event subscription. HttpProvider does not support event subscription btw.
    – viz
    May 27, 2018 at 12:56
  • i have this: var http = require('http');, is this what you mean? I am using express also. I also have websocket installed.. Could that help in anyway? I don't have a lot of back-end experience
    – jasper
    May 27, 2018 at 13:01
  • 1
    I mean when you initiate web3 instance, what kind of provider do you pass into it?
    – viz
    May 27, 2018 at 13:27
  • My ganache location which is localhost:7545, is this the issue? What would be an solution?
    – jasper
    May 27, 2018 at 13:32
  • I guess you are using HttpProvider then. See web3js.readthedocs.io/en/1.0/web3.html#value. You can use beta version of ganache-cli for now. github.com/trufflesuite/ganache-cli/issues/…
    – viz
    May 27, 2018 at 13:43

1 Answer 1

3

Former HttpProvider and ganache does not support web3 1.0's event subscription. Please check out the documentation. For testing purpose for now, you may use the beta version of ganache-cli with WebsocketProvider.

1
  • I'll try this out tonight
    – jasper
    May 28, 2018 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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