ngx-message-queue
A message queue library for Angular 2+ application components to communication each other.
ngx-message-queue library supports message headers and message payload. It also supports message selectors for subscribers to subscribe to only messages with specific message header via msgSelector.
Selectors are a way of attaching a filter to a subscription to perform content based routing. Selectors are typically apply to message headers, If no headers are attached to the message then all the messages will be delivered to subscriber by default. If subscriber is not configuring the msgSelector then all the messages will be delivered to the subscribers i.e. no filters are applied.
Multiple conditions are allowed in the msgSelector.
ngx-message-queue was previously knowns as ng2-message-queue.
Multiple conditions are allowed in the msgSelector.
ngx-message-queue was previously knowns as ng2-message-queue.
Example of a message selectors
LogLevel=fatalage > 13
age>=13 & gender=MFilter expression can be grouped using parenthesis |
Supported Operators
=, !=, >, <, >=, <=, &, |
|
Message header supports only JSON object. Multiple header properties can be posted with single JSON object
Message payload can be of any type (string, integer, object, JSON etc)
Index
- Install
- Usage
- Import into Angular 2+ application (typescript)
- API
- createQueue(name: string): boolean
- deleteQueue(name: string): boolean
- getQueueNames(): string[]
- getSubscribers(): string[]
- publish(name: string, headers: any, message: any, lazy = true): boolean
- subscribe(name: string, msgSelector: string, callback: (any, any) => void, lazy = true): string
- unsubscribe(id: string): boolean
- Example
- License
Install
npm install ngx-message-queue
Usage
Import into Angular 2+ application (typescript)
ngx-message-queue is implemented as Angular 2+ injectable service name NgxMessageQueue.
For apps using angular version 6+
NgxMessageQueue is Injectable as root so no additional decalaration is required in any module.
For apps using angular version prior to 6
Add
NgxMessageQueue into module providers.import { NgxMessageQueue } from 'ngx-message-queue';
@NgModule({
providers: [NgxMessageQueue]
})
Each component to use NgxMessageQueue
import { NgxMessageQueue } from 'ngx-message-queue';
export class MyComponent {
constructor(private messageQueue: NgxMessageQueue) { }
}
API
createQueue(name: string): boolean
createQueue will create queue name.
Return
false if queue name exist.this.messageQueue.createQueue('myQueue');
deleteQueue(name: string): boolean
deleteQueue will delete queue name.
Return
false if queue name does not exist.this.messageQueue.deleteQueue('myQueue');
getQueueNames(): string[]
getQueueNames will return all queue name in string array.let qNames: string[] = this.messageQueue.getQueueNames();
getSubscribers(): string[]
getSubscribers will return all subscribers id in string array.let ids: string[] = this.messageQueue.getSubscribers();
publish(name: string, headers: any, message: any, lazy = true): boolean
publish will put message into queue name. It will also put headers into queue if any. headers are optional but it is best way to process/route/filter the messages quickly without parsing the message payload.
If
lazy = true(default), queue name will be created automatically if not exist yet.
Return true if successful.
Return false if any of following is true:
lazy = false, and queuenamedoes not exist.nameis undefined.messageis undefined.
// lazy mode
message = 'This is a test message';
this.mq.publish('myQueue', {}, message);
subscribe(name: string, msgSelector: string, callback: (any, any) => void, lazy = true): string
subscribe will subscribe for the message posted on the queue name. Whenever queue name receives a new message, callback will be invoked. The callback will return both headers and message payload.
Subscribers can
subscribe to only certain messages within the same queue. Let us say you have multiple subscribers listening for log messages with different logging level (debug, info, warn, fatal etc). If you want to configure high priority subscriber who listens for logs with 'fatal' level then use msgSelector as loglevel=fatalthis.mq.subscribe('myQueue', 'loglevel=fatal', (headers, message) => this.handleMessage(headers, message));
If
lazy = true(default), queue name will be created automatically if not exist yet.
Return subscription id if successful.
Return empty string if any of following is true:
lazy = false, and queuenamedoes not exist.nameis undefined.callbackis undefined.
Either use Lambda(fat arrow) in typescript to pass in callback or bind
this to another variable in javascript, else this scope will be lost.
Lambda(fat arrow)
ngOnInit() {
this.mq.subscribe('testqueue', '', (headers, message) => this.handleMessage(headers, message));
}
handleMessage(headers, message) {
console.log(message);
}
unsubscribe(id: string): boolean
unsubscribe will cancel subscription using id.unsubscribe will return false if id is undefined or id is not found in subscription list.id: string;
this.messageQueue.unsubscribe(this.id);
Example
Github: ng2-message-queue-example