Monday, October 22, 2018

Angular Message Queue for Asynchronous Processing (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

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)



npm install ngx-message-queue


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';

 providers: [NgxMessageQueue]
Each component to use NgxMessageQueue
import { NgxMessageQueue } from 'ngx-message-queue';

export class MyComponent {

 constructor(private messageQueue: NgxMessageQueue) { }



createQueue(name: string): boolean
createQueue will create queue name.
Return false if queue name exist.
deleteQueue(name: string): boolean
deleteQueue will delete queue name.
Return false if queue name does not exist.
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';'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'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() {'testqueue', '', (headers, message) => this.handleMessage(headers, message));

handleMessage(headers, 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;




  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

  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.

  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.

    Online affiliates


  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


  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

  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

  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

  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

  9. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
    data science course in mumbai

  10. Nice post. Thanks for sharing! I want people to know just how good this information is in your blog. It’s interesting content and Great work.
    machine learning courses
    data science course in hyderabad
    business analytics courses in hyderabad

  11. This is a wonderful article, Given so much info in it, Thanks for sharing. CodeGnan offers courses in new technologies and makes sure students understand the flow of work from each and every perspective in a Real-Time environmen python training in vijayawada. , data scince training in vijayawada . , java training in vijayawada. ,

  12. Impressive! I finally found great post here. Nice article on data science . It's really a nice experience to read your post. Thanks for sharing your innovative ideas to our vision.
    Data Science Course
    Data Science Course in Marathahalli

  13. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
    Data science Interview Questions
    Data Science Course

  14. I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day!
    Best Data Science training in Mumbai

    Data Science training in Mumbai

  15. Wow! This article is jam-packed full of useful information. The points made here are clear, concise, readable and poignant. I honestly like your writing style.
    SAP training in Kolkata
    Best SAP training in Kolkata
    SAP training institute in Kolkata

  16. Reading this article gave me many things to think about. You have some quality information here that any reader would enjoy. I share many of your views in this article.
    SAP training in Mumbai
    Best SAP training in Mumbai
    SAP training institute Mumbai

  17. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance
    Simple linear regression

  18. I am incessantly thought about this, thanks for putting up. website design company nyc

  19. Found your weblog and decided to have a study on it, not what I usually do, but this blog is wonderful. Awesome to see a site that’s not spammed, and actually makes some sense. Anyway, great write up. web design manhattan

  20. very nice post, i actually love this web site, keep on it branding agencies in san francisco

  21. Great post, you have pointed out some fantastic details , I besides believe this s a very great website. branding firms san francisco

  22. I like how this article is written. Your points are sound, original, fresh and interesting. This information has been made so clear there's no way to misunderstand it. Thank you.
    SAP training in Kolkata
    SAP training Kolkata
    Best SAP training in Kolkata
    SAP course in Kolkata
    SAP training institute Kolkata

  23. Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well.

    Data Science In Banglore With Placements
    Data Science Course In Bangalore
    Data Science Training In Bangalore
    Best Data Science Courses In Bangalore
    Data Science Institute In Bangalore

    Thank you..

  24. Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting. And I found this one pretty fascinating and it should go into my collection. Very good work!
    PMP Certification
    PMP Course
    PMP Course in Malaysia
    PMP Training in Malaysia
    PMP Training

  25. It is the intent to provide valuable information and best practices, including an understanding of the regulatory science course

  26. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
    data analytics course
    big data analytics malaysia
    big data course