Home

Awesome

Ng-Radio

RxJS-based message bus service for Angular2 apps inspired by Backbone.Radio. Inject it in your application module and have fun.

There is nothing angular2-specific, though. It is possible to use it in any application.

Installation

npm install --save ng-radio

NPM page

Usage

First, import it:

import { NgRadio } from 'ng-radio';

Then, if using Angular2, inject it as a service (do not forget about providers):

......
import { NgRadio } from 'ng-radio';
......

@NgModule({
    imports:[
		......
    ],
    providers: [
    	.......
        NgRadio,
        .......
    ],

constructor(private radio: NgRadio){...}

Or create an instance manually:

let radio = new NgRadio();

Since you have NgRadio instance in your app, you can use these methods for passing messages:

Patterns may contain multiple segments split by :. Use this feature to create namespaces for messages you cast. You can use * in pattern to subscribe to any matching segment, or use ** to subscribe to all segments, starting from particular position.

For example, you can use on('error:*') and subscribe to all errors, including something like error:http or error:internal and so on:

radio.cast('app:start',     'started');
radio.cast('message:greet', 'Hi!');
radio.cast('message:bye',   'Bye!');

radio.on('app:start').subscribe((message)=>{
	console.log(message); //will receive 'started' only
});

radio.on('message:greet').subscribe((message)=>{
	console.log(message); //will receive 'Hi!'
});

radio.on('message:bye').subscribe((message)=>{
	console.log(message); //will receive 'Bye!'
});

radio.on('message:*').subscribe((message)=>{
	console.log(message); //will receive both 'Hi!' and 'Bye!'
});

radio.on('**').subscribe((message)=>{
	console.log(message); //will receive all messages: 'started', 'Hi!' and 'Bye!'
});

Examples

These strings will match: