3

I have a very simple solidity function that just calls an event:

     function sendMessage(int id, string title, string message){
       Message(id, method, parameters);
     }

When I call it using web3:

communicationChannelInstance.sendMessage(45, 'exampleTitle', '{}');

I can see it with an event watch just fine.

The problem comes when I call that functions multiple times inside a loop:

      ids.forEach(function(id) {
        communicationChannelInstance.sendMessage.sendTransaction(id, 'exampleTitle', '{}');
      })

No matter how many times this loop is called, it only shows the latest event.

This is the watch I'm using:

var events = contractInstance.allEvents();
  events.watch(function(error, event){
      if (error) {
        callback(error, null);
      } else {
         var eventRet = event;
        callback(null, eventRet);
      }
  });

Why does this happen? How can I fire multiple events really fast and execute all of them?

Thanks for the help.

  • Can you show us the actual failing code, ie with the watch etc? – Edmund Edgar Aug 2 '17 at 8:51
  • Are you sure all the transactions were executed? Also how many transaction you were working with? – Ismael Aug 2 '17 at 13:11
  • All the transactions are executed, if I check for all the events I see them all, but if I'm checking for the most recent ones and fire the events without a wait between them I only get the latest event. – Enrique Alcazar Aug 2 '17 at 14:36
  • @Ismael This happens with just 2 events in the foreach, And all the transactions where executed, I can see them when I query all the events fired, I just can't see them realtime – Enrique Alcazar Aug 4 '17 at 9:45
1

Solved it, it's a problem with Ganache/testRPC and web3, if it's set to mine transactions as soon as they get them instead of using the flag to mine blocks --blockTime the event reader gets confused and doesn't pick up the transactions properly.

In short, set a --blockTime of 3+ seconds and it works.

0

I don't know if it is the same case, but I had a similar issue. I used web3js with MetaMask. When I used this funcion to log events

mycontract.MyEvent({fromBlock:0, toBlock: 'latest'}, function(error, log){...});,

it only shows the latest event; instead, using

mycontract.allEvents({fromBlock:0, toBlock: 'latest'}, function(error, log){
   if (!error){
       if(log.event == "Message"){
            console.log(log.args.id);
            console.log(log.args.method);
            console.log(log.args.parameters);
       }
   }
});

it shows all.

  • Your system does show all the messages when executed. What I don't understand is why when I run the system first, followed by the loop -it only reads the latest event thrown, not all of them, however when i run the system after the loop - it does show all events. – Enrique Alcazar Aug 2 '17 at 11:22
  • From the documentation github.com/ethereum/wiki/wiki/JavaScript-API#contract-events var myEvent = myContractInstance.MyEvent({some: 'args'}, {fromBlock: 0, toBlock: 'latest'}); var myResults = myEvent.get(function(error, logs){ ... }); will get all existing events, and myEvent.watch(function(error, result){ ... }); will get you only the recent ones. – Ismael Aug 2 '17 at 13:15
  • @Ismael I've used both, when I try to get the recent ones, if multiple events are fired really close to one another, the second one you used only shows the latest one, if for example I wait 5 sec between each event fire, it shows all of them. – Enrique Alcazar Aug 2 '17 at 14:35

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.