You just need to create some service that will emit messages on which you can subscribe. It can be Observable
from rxjs, EventEmitter
from node.js, or anything else that follows Observable pattern. Then you should use Dependency Injection to inject this service into concrete components. See this plunker.
class Broadcaster extends EventEmitter {}
@Component({
selector: 'comp1',
template: '<div>Generated number: {{ generatedNumber }}</div>',
})
class Comp1 {
generatedNumber: number = 0;
constructor(broadcaster: Broadcaster) {
setInterval(() => {
broadcaster.next(this.generatedNumber = Math.random());
},1000);
}
}
@Component({
selector: 'comp2',
template: '<div>Received number: {{ receivedNumber }}</div>',
})
class Comp2 {
receivedNumber: number = 0;
constructor(broadcaster: Broadcaster) {
broadcaster.observer({
next: generatedNumber => this.receivedNumber = generatedNumber
});
}
}
@Component({
selector: 'app',
viewProviders: [Broadcaster],
directives: [Comp1, Comp2],
template: `
<comp1></comp1>
<comp2></comp2>
`
})
export class App {}
PS In this example I use EventEmitter
from angular2, but again, it can be whatever you want