Monday, October 22, 2018

Angular Message Queue for Asynchronous Processing (ngx-message-queue)

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.
Example of a message selectors
LogLevel=fatal

age > 13

age>=13 & gender=M

Filter expression can be grouped using parenthesis

age<=5 & ((gender=M & state=NJ) | gender=F)
Supported Operators
=, !=, >, <, >=, <=, &, |
Message header supports only JSON object. Multiple header properties can be posted with single JSON object
{
"id": "1234567890",
"age": "20"
}

Message payload can be of any type (string, integer, object, JSON etc)

Index

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 queue name does not exist.
  • name is undefined.
  • message is 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=fatal
this.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 queue name does not exist.
  • name is undefined.
  • callback is 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

12 comments:

  1. Wow!! Really a nice Article. Thank you so much for your efforts. Definitely, it will be helpful for others. I would like to follow your blog. Share more like this. Thanks Again.
    iot training in Chennai | Best iot Training Institute in Chennai

    ReplyDelete
  2. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to article very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    DedicatedHosting4u.com

    ReplyDelete
  3. Great. It is good to constantly coming up with creative ideas. Provides much needed knowledge. goal oriented blog posts and always tried to find creative ways to meet goals.

    Thanks
    Online affiliates

    ReplyDelete

  4. Your post is very good. I got to learn a lot from your post. Thank you for sharing your article for us. it is amazing post
    what is seo
    types of seo

    ReplyDelete

  5. This is a fantastic idea! I like it a lot because it's super easy for the audience to see the value of opting in. wonderful and amazing post very use full your post thanks for sharing your article
    Android Application development
    Web application

    ReplyDelete
  6. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.
    Software testing online training
    Software testing certification training
    Software testing online course
    Software testing training course

    ReplyDelete
  7. Attend The Data Analytics Course From ExcelR. Practical Data Analytics Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Course.
    ExcelR Data Analytics Course

    ReplyDelete
  8. Attend The PMP Certification From ExcelR. Practical PMP Certification Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The PMP Certification.
    ExcelR PMP Certification

    ReplyDelete