You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> info **Hint** Import the `EventEmitter2` from the `@nestjs/event-emitter` package.
62
+
63
+
Then use it in a class as follows:
64
+
65
+
```typescript
66
+
this.eventEmitter.emit(
67
+
'order.created',
68
+
newOrderCreatedEvent({
69
+
orderId: 1,
70
+
payload: {},
71
+
}),
72
+
);
73
+
```
74
+
75
+
#### Listening to Events
54
76
55
77
To declare an event listener, decorate a method with the `@OnEvent()` decorator preceding the method definition containing the code to be executed, as follows:
Note that such a wildcard only applies to one block. The argument `order.*` will match, for example, the events `order.created` and `order.shipped` but not `order.delayed.out_of_stock`. In order to listen to such events,
105
+
use the `multilevel wildcard` pattern (i.e, `**`), described in the `EventEmitter2`[documentation](https://github.com/EventEmitter2/EventEmitter2#multi-level-wildcards).
83
106
84
-
To dispatch an event, first inject `EventEmitter2` using standard constructor injection:
107
+
With this pattern, you can, for example, create an event listener that catches all events.
> info **Hint** Import the `EventEmitter2` from the `@nestjs/event-emitter` package.
91
-
92
-
Then use it in a class as follows.
93
-
94
-
```typescript
95
-
this.eventEmitter.emit(
96
-
'order.created',
97
-
newOrderCreatedEvent({
98
-
orderId: 1,
99
-
payload: {},
100
-
}),
101
-
);
110
+
@OnEvent('**')
111
+
handleEverything(payload: any) {
112
+
// handle and process an event
113
+
}
102
114
```
103
115
104
116
> info **Hint**`EventEmitter2` class provides several useful methods for interacting with events, like `waitFor` and `onAny`. You can read more about them [here](https://github.com/EventEmitter2/EventEmitter2).
0 commit comments