Creating Event Emitters

Effective integrations in your backend can be achieved using events, similar to how webhooks work. An event emitter will inform you about any events that could require actions to be performed like missing a block, saving you from repeatedly querying the API.

Emitting Events

Emitting events is pretty straightforward. Just inject the EventDispatcherService :

1@Container.inject(Container.Identifiers.EventDispatcherService)
2private readonly events!: Contracts.Kernel.EventDispatcher;

And call the dispatch method with a name and data to be emitted :

1this.events.dispatch("block.forged", {
2 id: "fake-id",
3 generatorPublicKey: "fake-generator-public-key",
4 amount: 10
5});

Listening to Events

Listening to events is as straightforward as emitting them. Just inject the EventDispatcherService :

1@Container.inject(Container.Identifiers.EventDispatcherService)
2private readonly events!: Contracts.Kernel.EventDispatcher;

And call the listen method with a name and then process the incoming data:

1this.events.listen("block.forged", block => {
2 if (block.generatorPublicKey === "fake-generator-public-key") {
3 console.log(`You just forged a block for ${block.amount} ARK`);
4 }
5});

Available Core Events

Have a look at the events in the core kernel enums:

Last updated 2 years ago
Edit Page
Share: