Rxmq.js
JavaScript pub/sub library based on RxJS
What is it?
Rxmq.js is an in-memory message bus based on reactive extensions - inspired by postal.js - written in JavaScript using ES6 and Babel. Rxmq.js runs equally good in the browser and on the server using node.js. It provides a 'broker' that allows for creation of more sophisticated pub/sub implementations than what you usually find in event-style based libraries. On top of that, all used objects are parts of reactive extensions which allows doing a lot of cool things with them out of the box.
Quick start
If you want to subscribe to an observable, you tell Rxmq what channel and topic to subscribe to and a set of functions to be invoked (taken from Rx.Observable.subscribe):
; const subscription = Rxmq ;
The publisher might do something similar to this:
Rxmq next title: 'Woo-hoo, first post!' text: 'My lengthy post here' ;
Note, that if you are not using ES6 modules (e.g. with babel), you will need to require Rxmq in the following way:
var Rxmq = default;
Channels? Topics?
A channel is a logical partition of topics, more specifically - a set of topics.
As well explained by postal.js readme section on channels, conceptually, it's like a dedicated highway for a specific set of communication.
In case of Rxmq.js each topic is represented by a slightly tweaked Rx.Subject (specifically - it never triggers complete()
, so you can keep sending your data all the time).
Using channel- and topic-oriented messaging instead of traditional JavaScript approaches like callbacks or promises enables you to separate components (or modules) communication by context.
It's possible to get a more concise API if you want to hang onto a Channel
- which can be really convenient while working with a specific channel (e.g. inside of a specific component):
const channel = Rxmq;const subject = channel; const subscription = subject; subjectnext title: 'Woo-hoo, first post!' text: 'My lengthy post here';
How's Rxmq.js Different From {Insert Eventing Library Here}?
Some of those are shamelessly taken from postal.js list :)
- Rxmq is not an event emitter - it's not meant to be mixed into an instance. Instead, it's a stand alone 'broker' – a message bus.
- Rxmq uses a slightly modified Rx.Subject (it will never be completed or stopped by error) to pass messages. This means you use all the cool features of Rx.Observable and Rx.Observer while working on your messaging.
- Most 'event aggregator' libs are single channel - which can lead to event name collision, and reduce the performance of matching an event to the correct subscribers. Rxmq is multi-channel.
- Rxmq built-in topic logic supports hierarchical wildcard topic bindings - supporting the same logic as topic bindings in the AMQP spec. And if you don't like that approach, you can easily provide your own bindings resolver.
More on How to Use It
Here are four examples of using Rxmq.
// This gets you a handle to the default Rxmq channel// You can get a named channel instead like this:// const channel = Rxmq.channel('DoctorWho');const channel = Rxmq; // subscribe to 'name.change' topicsconst subscription = channel; // And someone publishes a name change:channelnext name: 'Dr. Who' ; // To dispose, just trigger the unsubscribe() method:subscription;
Subscribing to a wildcard topic using *
The *
symbol represents 'one word' in a topic (i.e - the text between two periods of a topic).
By subscribing to '*.changed'
, the binding will match name.changed
& location.changed
but not changed.companion
.
const chgSubscription = channel;channelnext type: 'Name' value: 'John Smith' ;channel next type: 'Location' value: 'Early 20th Century England' ;chgSubscription;
Subscribing to a wildcard topic using #
The #
symbol represents 0-n number of characters/words in a topic string. By subscribing to 'DrWho.#.Changed'
, the binding will match DrWho.NinthDoctor.Companion.Changed
& DrWho.Location.Changed
but not Changed
.
const starSubscription = channel;channel next type: 'Companion Name' value: 'Rose' ;channel next type: 'Companion Name' value: 'Martha' ;channel next type: 'Companion Name' value: 'Amy' ;channel next type: 'Location' value: 'The Library' ;channel next type: 'DrumBeat' value: "This won't trigger any subscriptions" ;channelnext type: 'Useless' value: "This won't trigger any subscriptions either";starSubscription;
Using Rx.Observable methods with a subscription
; const dupChannel = Rxmq;const dupSubscription = dupChannel ;// demonstrating multiple channels per topic being used// You can do it this way if you like, but the example above has nicer syntax (and *much* less overhead)dupChannelnext value: "Don't Blink" ;dupChannelnext value: "Don't Blink" ;dupChannel next value: "Don't Even Blink" ;dupChannel next value: "Don't Close Your Eyes" ;dupChannelnext value: "Don't Blink" ;dupChannelnext value: "Don't Blink" ;dupSubscription;
Using request-response pattern
To make a request, you can do the following:
const channel = rxmq; channel ;
It's also possible to make a request with custom reply subject, like so:
const channel = rxmq; channel ;
To handle requests:
// SUCCESS REPLYconst subscription = channel ; // ERROR REPLYconst subscription = channel ;
Make sure to always call .complete()
after you're done with dispatching your data.
Connecting external Rx.Observable to Rxmq topic
const topic = channel;const ajax = RxObservable;ajax;
Available plugins
- rxmq.aliases - a plugin that provides bus- and channel-level convenience aliases.
- rxmq.middleware - a plugin that adds support for topic-based middleware.
I still need help!
Feel free to ask any questions you might have by opening an issue.
Build, Dependencies, etc.
- Rxmq depends only on RxJS.
Can I contribute?
Sure thing! While project is still in its early days, I hope the API is relatively stable. Pull requests are welcome, but please make sure to include tests for your additions.