-
Notifications
You must be signed in to change notification settings - Fork 0
NestJS : How to emit an event
Mathieu edited this page May 24, 2022
·
7 revisions
Before to trigger an event, we suggest you to read this article : NestJs Event - Event Emitter package.
EventEmitter2 is used to emit and listening an event. It's possible to see code production samples inside the products.service.ts.
For example, after the creation of a product, the line bellow is used:
this.eventEmitter.emit('product.created', product);This emit product.created, it means than a product has been created.
| Type | Describe | Note |
|---|---|---|
| product.created | A product has been created | |
| product.retrieve | A product has been retrieved by someone | |
| product.warranty.extend | An product's warranty has been extended | |
| product.status.update | A product's status has been updated | |
| commercial.event | A commercial event has been emited |
The listener will read the event emited and create an event on the database and the blockchain. This event shall have an event type
Example :
//Handle and process on created
@OnEvent('product.created')
handleCreatedEvent(product) {
const event = {
content: 'A product has been created',
type: 'PRODUCT_CREATED',
};
this.saveEventOnDataBase(event, product);
this.saveEventOnBlockchain(event, product);
}You may have seen than an event type and an event emited type have common name. It's because the event emited type will trigger the creation of an event that contains an event and a product object.