3

I have a test contract that emits events. I'd like to create a web app that shows a list of all created events + new ones as they arrive. I'm using Meteor app connected to geth 1.4.10 through rpc. The code is:

var filter = web3.eth.filter({fromBlock:0, toBlock: 'latest', address: contract.address, 'topics':[topic]});
filter.watch(function(error, result) {
  // attach event to list
  console.log("Event: " + result);
});

It seems to work, however there's a huge (above a minute) delay to get the events that are already created in the callback. I'd like to show a list immediately or almost immediately when the page is loaded and then keep updating as new events come. Is this the correct way? How can I reduce the delay?

  • fromBlock could probably start at the block at which the contract was deployed. – Xavier Leprêtre B9lab Sep 19 '16 at 15:00
  • @XavierLeprêtreB9lab Thanks! This did a trick it now works immediately. – lgasior Sep 19 '16 at 15:11
  • 1
    Note that this solution isn't perfect-- as time goes on, the process will become slower and slower. Do you need to show all previous events? It might make sense to show only events from the last 10,000 blocks, then go back farther upon request. (i.e. a "Show more" button ) – Tjaden Hess Sep 19 '16 at 15:34
  • Also, @XavierLeprêtreB9lab you should post this as an answer, since it does fully answer the question. – Tjaden Hess Sep 19 '16 at 15:38
  • Thanks for pointing that out @TjadenHess. I was thinking about that too. As more blocks come "Show more" button may be the way to go, but it won't be perfect thought, especially when there'll be a number of blocks without any event (I guess in such case "Show more" may get unresponsive). Another possible solution I can think of is just caching the results, thanks to the blockchain being immutable. – lgasior Sep 19 '16 at 21:18
1

fromBlock could probably start at the block at which the contract was deployed.

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.