Ethereum DApps: How to Listen for Events

Figure 4: Subscribe to ‘MyEvent’

As stated earlier, the event we want to listen for is called MyEvent, and figure 4 shows us how. Our myContract instance has an accessor called events from which we can target MyEvent. This is an asynchronous function that returns a promise, and it’s called every time MyEvent is emitted.

Line 3 defines what happens once data has been returned from the promise. Here we’re simply logging the event values to the console, but this is where we’d code features like alerts or interface updates to update the user on the event that’s just occurred.

Using this logic, every MyEvent emitted from MyContract will be caught here.