When an event (Something) is triggered in a contract, the page console logs the output as intended (through somethingEvent.watch()), but when I reload the page, it console logs the same output. Is the most recent event meant to persist or am I not using it properly? Thanks.
// Solidity
event Something(string message);
function doSomething(string newMessage) payable {
Something(newMessage);
}
// Web3
var somethingEvent = contractInstance.Something();
somethingEvent.watch(function(error, result) {
console.log(result.args.message);
});
This will happen whenever you call
watch()
and the latest block number is the same as the previous execution. When an event is "fired", all the EVM is doing is writing out the event information to the log once the transaction is successfully mined.When you add a watch to a contract, you can specify which block you want to start listening on. If you don't pass in anything (as your code is doing), it defaults to the latest block (see filter API docs). So, when you reload the page and a new block hasn't been added to the chain, you will get the same event.
If you want to avoid duplicate events, use
result.blockNumber
to remember the last block read and increment it by 1 whenever establishing a new watch.Note that
watch
may not be the best thing to use in this case. It depends on how stateful your client is. You should always callstopWatching
whenever you're done listening for events. If you don't want to set up a watch, you can also useget
which will send back all events that match your filter criteria.