3

In my app i have multiple listeners (watchers) on different events of same contract. Nothing complicated.

let songAddedEvent = instance.SongAdded({}, {
        fromBlock: 'latest',
        toBlock: 'latest'
    });

songAddedEvent.watch(function (error, result) {
    if (error) {
        console.error(error);
    }
    console.log(result);
}
-------------
/////////////////////////////////////////
-------------------------
let newVoteEvent = instance.NewVote({}, {
        fromBlock: 'latest',
        toBlock: 'latest'
    });
newVoteEvent.watch(function (error, result) {
    if (error) {
        console.error(error); 
    }
    console.log(result);
}

Problem is that same event is caught multiple times, and strange thing is that event is caught exactly number_of_listeners times.

So in this case i have two listener on two different events and triggered listener is triggered 2 times

Tried with 3 listeners, then is triggered 3 times.

(Not all of them are triggered, just one listener multiple times).

I read this article https://github.com/ethereum/web3.js/issues/398 but doesn't explain why there is correlation between number of listeners and multiplication of event triggers.

Forgot to mention, console.log is called from same .js file and same line in that file.

Does somebody have an idea why is this happening??

  • Shouldn't the second watch binded to newVoteEvent instead of songAddedEvent? In your code you binded two watchers to the same event so it's normal you trigger two responses – Marco Ottolini Nov 21 '18 at 20:44
  • I made mistake with copying code. I corrected it. Again I sad that console.log is called always from same line, so its always same wather. – Nedeljko Kuzmanović Nov 22 '18 at 7:31
  • I have the exact same problem. – brendan Jan 23 at 21:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.