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
Copy file name to clipboardExpand all lines: README.md
+235-16
Original file line number
Diff line number
Diff line change
@@ -1,27 +1,84 @@
1
1
2
-
3
2
# Angular PubSub
4
3
5
4
Angular 11.x implementation of the [publish subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) Pattern.
6
5
7
6
   [](https://badge.fury.io/js/%40fsms%2Fangular-pubsub)    [](https://david-dm.org/FullStackMaster1/fsms-angular-pubsub)  
8
7
9
-
## Installing
8
+
> By [Rupesh Tiwari](https://rupeshtiwari.com)
9
+
10
+
**If you enjoy @fsms/angular-pubsub, please consider [supporting me](https://github.com/sponsors/rupeshtiwari) for years of development (and to unlock rewards!) ❤**
this.pubsubService.subscribe({ 👈// Subscribing to a message
142
+
messageType: PlaceOrder.messageType,
70
143
callback: (msg) =>console.log('received', msg),
71
-
});
144
+
})
145
+
);
146
+
}
72
147
}
73
148
```
74
-
4.**Publishing Message**
75
-
The `publish` method takes one argument where it expect the `message` object.
149
+
4.**Publishing a Message**
150
+
The `publish` method takes one argument where it expect the `Message` object.
76
151
77
152
Example: Now on a button click, I want to publish a message with some payload.
78
153
@@ -93,20 +168,164 @@ export class AppComponent {
93
168
orderPlaced($event:KeyboardEvent) {
94
169
$event.preventDefault();
95
170
96
-
this.pubsubService.publish(newOrderPlaced('20 Apples'));// <= HERE
171
+
this.pubsubService.publish( 👈// Publishing a message
172
+
newOrderCreated({
173
+
orderId: newDate().getTime().toString(36),
174
+
item: '20 Apples',
175
+
})
176
+
);
97
177
}
98
178
}
99
179
```
100
180
5.**Unsubscribing Messages**
101
181
182
+
Keep all subscriptions per component in an array. And On component you must unsubscribe your subscriptions on `ngOnDestroy` event.
183
+
184
+
```ts
185
+
ngOnDestroy(): void {
186
+
this.subscriptions.forEach((s) =>s.unsubscribe());👈// Unsubscribing a message
187
+
}
188
+
```
189
+
190
+
## Using Angular Service As Message Handler
191
+
192
+
Convert Angular service to a Message Handler. If to organize your angular code base as Service Oriented Architecture (SOA) way. And you want to create an Angular service that can listen to a Message and react on them just like a N-ServiceBus Message Handlers?
193
+
194
+
Then you must use `@RegisterHandler({})` decorator on any Angular Service then it will automatically be registered as message subscriber. This helps us to organize your business logic in services rather in angular components.
195
+
196
+
**Message Handler**
197
+
198
+
Message handler is a service that can listen to one message or more messages and perform business logic. Message Handler can also publish after handling incoming messages.
199
+
200
+
Diagram of a Angular Service as Message Handler called as `ShipOrderService` which listens to `OrderReady` message and process shipping then publishes `OrderShipped` message.
201
+
202
+

203
+
204
+
205
+
**Creating Message Handler at Root Module**
206
+
207
+
1. First create your message handler at Root (App) Module.
208
+
209
+
Example: When Order is Ready Shipping service is starting the shipment process.
**Creating Message Handler at Feature Module Level**
264
+
265
+
In order to achieve true service oriented architecture. You must create independent isolated feature modules. The message handlers gives you the power to register your message handlers at feature module level.
266
+
267
+
1. First create your message handler at Feature Module.
268
+
269
+
Example: `Create Order` Message handler in `Orders` module.
PubsubModule.forFeature([CreateOrderService]) 👈 // Registering as feature message handler
314
+
],
315
+
exports: [SubmitOrderComponent],
316
+
})
317
+
exportclassOrdersModule {}
318
+
```
319
+
320
+
## Contributions
102
321
103
-
---
322
+
Contributions are welcome!🙂 If you find any problems or would like to contribute in any way, feel free to create a pull request/open an issue/send me a message.
104
323
105
-
**Thank You!**
324
+
You can also contribute by becoming an [official sponsor](https://github.com/sponsors/rupeshtiwari) to help keep Angular Pub-Sub well-maintained.
0 commit comments