Intercom Service provides internal communication between app components
npm install ngx-intercomIntercom Service provides internal communication between app components
You can call it and use as "simplest state management",
then there is no need to use NgRx / Akita / etc.
or implement your own RxJs service for that.
Because it's already done for you!


npm i ngx-intercom --save
``typescript
import { IntercomModule } from 'ngx-intercom';
// in app.module.ts
@NgModule( {
imports: [
...
IntercomModule.forRoot({
useLocalStorage: true, // optional, false by default
forceUpdate: false, // optional, false by default
})
...
]
} )
export class AppModule { ... }
// in your first component
import { IntercomService } from 'ngx-intercom';
@Component( {
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: [ './home.component.scss' ]
} )
class HomeComponent implements OnInit {
testMessage;
constructor(private intercom: IntercomService){}
ngOnInit() {
let counter = 1;
setInterval(() => {
this.testMessage = Test Message-${counter++};
this.intercom.push('testMessage', this.testMessage);
console.log(this.testMessage);
}, 1000);
this.intercom.push('something', 'something');
setTimeout(() => {
this.intercom.push('and-other-messages', 'and-other-messages');
}, 3000);
}
onSomethingChange($event)
{
this.intercom.push('something', $event.something);
console.log( $event.something )
}
}
// in your other component
import { IntercomService } from 'ngx-intercom';
import { Subscription } from 'rxjs';
@Component( {
selector: 'app-other',
templateUrl: './other.component.html',
styleUrls: [ './other.component.scss' ]
} )
class OtherComponent implements OnInit, OnDestroy {
something: any;
message: any;
testMessage$: Observable
private onDestroy$: Subject
constructor(private intercom: IntercomService){
}
ngOnInit() {
// It's preferable!
// then in .html read this way:
//
// ... and this way also available
//
this.intercom
.read( [ 'something', 'and-other-messages' ] )
.pipe(
takeUntil(this.onDestroy$),
)
// or .read ( 'something' ) - if only one issue to be watched
.subscribe( data => {
if (data['something']) {
this.something = data['something'];
}
if (data['and-other-messages']) {
this.message = data['and-other-messages'];
}
});
}
onAnythingChange($event)
{
this.intercom.push('anything', $event.anything);
console.log($event.anything)
}
ngOnDestroy(){
this.onDestroy$.next();
}
}
`
`typescript`
export interface IntercomData
[key: string]: T;
}
`typescript
/**
* Push the new message / state (channel : value),
* if useLocalStorage === true - localStorage will be used to store data (false by default)
* if forceUpdate === true - will be forcebly repeated even it's duplicate (false by default)
*/
push
/**
* Read the stream of messages / state changes (channel : value), if channels are empty - read all the messages / state changes, otherwise - only specified
*/
read
/**
* Read the last value of message / state by channel specified
*/
last
/**
* Remove the message / state by channel specified
*/
remove( channel: string ): boolean
``
Any pull-request is more than welcome :boom: :smile:
This project adheres to the Contributor Covenant code of conduct. By participating, you are expected to uphold this code.
MIT