1- [ ![ Actions Status] ( https://github.com/rupeshtiwari/fsms-angular-pubsub/workflows/.github/workflows/main.yml/badge.svg )] ( https://github.com/rupeshtiwari/fsms-angular-pubsub/actions )
21
3- # Angular Pub/Sub Framework for Angular versions
42
5- Angular publish subscribe framework written by using ` RxJS ` only.
3+ # Angular PubSub
64
7- ## Installing Package
5+ Angular 11.x implementation of the [ publish subscribe ] ( https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern ) Pattern.
86
9- - Run below to install
7+    [](https://badge.fury.io/js/%40fsms%2Fangular-pubsub)    [](https://david-dm.org/FullStackMaster1/fsms-angular-pubsub)  
108
11- ```
9+ ## Installing
10+
11+ ** npm installation**
12+
13+ ``` shell
1214npm i -S @fsms/angular-pubsub
1315```
14- ## Using Pub Sub
16+ ## Using PubSub For Inline Style
1517
16- 1 . Importing Module
18+ 1 . ** Importing PubsubModule in application module ** .
1719
1820Initialize module for root in your angular root module
1921
@@ -39,64 +41,72 @@ bootstrap: [RootComponent]
3941
4042```
4143
42- 2 . Subscribing to Message
44+ 2 . ** Injecting ` PubsubService ` as dependency in component **
4345
4446Go to desired component and subscribe to a message.
4547
4648``` ts
4749import { Component } from ' @angular/core' ;
48- import { PubSubService } from ' @fsms/angular-pubsub' ;
49- import { PlaceOrder , PlaceOrderType } from ' ./placeorder-message' ;
50+ import { PubsubService } from ' @fsms/angular-pubsub' ;// <= HERE
5051
5152@Component ({
5253 selector: ' app-root' ,
5354 templateUrl: ' ./app.component.html' ,
5455 styleUrls: [' ./app.component.css' ],
5556})
5657export class AppComponent {
57- constructor (private messageService : PubSubService ) {
58- this .messageService .subscribe ({ // <= HERE
59- messageType: PlaceOrderType ,
60- callback : (msg ) => console .log (' received' , msg ),
61- });
62- }
58+ constructor (
59+ private pubsubService : PubsubService /* <= HERE */ ) {}
6360}
6461```
65- 3 . Publishing Message
62+ 3 . ** Subscribing to message**
63+
64+ In ` ngOnInit ` method of angular, you can subscribe to the events that you want to react upon.
6665
67- Now on a button click, I want to publish a message with some payload.
66+ ``` ts
67+ ngOnInit (): void {
68+ this .pubsubService .subscribe ({ // <= HERE
69+ messageType: PlaceOrderType ,
70+ callback : (msg ) => console .log (' received' , msg ),
71+ });
72+ }
73+ ```
74+ 4 . ** Publishing Message**
75+ The ` publish ` method takes one argument where it expect the ` message ` object.
76+
77+ Example: Now on a button click, I want to publish a message with some payload.
6878
6979``` ts
7080import { Component } from ' @angular/core' ;
71- import { PubSubService } from ' @fsms/angular-pubsub ' ;
72- import { PlaceOrder , PlaceOrderType } from ' ./placeorder-message ' ;
81+ import { OrderPlaced } from ' ./messages/placeorder-message ' ;
82+ import { PubsubService } from ' @fsms/angular-pubsub ' ; // <= HERE
7383
7484@Component ({
7585 selector: ' app-root' ,
7686 templateUrl: ' ./app.component.html' ,
7787 styleUrls: [' ./app.component.css' ],
7888})
7989export class AppComponent {
80- constructor (private messageService : PubSubService ) {
81- this .messageService .subscribe ({
82- messageType: PlaceOrderType ,
83- callback : (msg ) => console .log (' received' , msg ),
84- });
85- }
90+ constructor (
91+ private pubsubService : PubsubService /* <= HERE */ ) {}
8692
87- sendMessage ($event : KeyboardEvent ) {
93+ orderPlaced ($event : KeyboardEvent ) {
8894 $event .preventDefault ();
89- this .messageService .publish (new PlaceOrder (' 20 Apples' ));// <= HERE
95+
96+ this .pubsubService .publish (new OrderPlaced (' 20 Apples' ));// <= HERE
9097 }
9198}
9299```
100+ 5 . ** Unsubscribing Messages**
101+
93102
103+ ---
94104
95- ### Thank You!
105+ ** Thank You!**
96106
97- ** 💖 Say 👋 to me!**
98- Rupesh Tiwari < br />
99- <a href =" https://www.rupeshtiwari.com " > www.rupeshtiwari.com </a > < br />
100- ✉️ <a href =" mailto:fullstackmaster1@gmail.com?subject=Hi " > Email Rupesh</a >< br />
107+ 💖 Say 👋 to me!
108+ Rupesh Tiwari
109+ <a href =" https://www.rupeshtiwari.com " > www.rupeshtiwari.com </a >
110+ ✉️ <a href =" mailto:fullstackmaster1@gmail.com?subject=Hi " > Email Rupesh</a >
101111Founder of <a href =" https://www.fullstackmaster.net " > Fullstack Master</a >
102112
0 commit comments