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

68 comments:

  1. Replies
    1. IEEE Final Year projects Project Center in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, project projects for cseAngular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
    2. IEEE Final Year projects Project Center in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, project projects for cseAngular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. 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
  3. 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
  4. 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

  5. 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

  6. 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
  7. 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
  8. 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
  9. 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
  10. 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

    ReplyDelete
  11. 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

    ReplyDelete
  12. 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. ,

    ReplyDelete
  13. 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

    ReplyDelete
  14. 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

    ReplyDelete
  15. 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


    ReplyDelete
  16. 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

    ReplyDelete
  17. 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

    ReplyDelete
  18. 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

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

    ReplyDelete
  20. 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

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

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

    ReplyDelete
  23. 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


    ReplyDelete
  24. 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..

    ReplyDelete
  25. 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

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

    ReplyDelete
  27. 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

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

    ReplyDelete
  29. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.

    Simple Linear Regression

    Correlation vs Covariance

    ReplyDelete
  30. 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
    data science interview questions

    ReplyDelete

  31. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
    360DigiTMG PMP Certification

    ReplyDelete
  32. 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

    ReplyDelete
  33. I am looking for and I love to post a comment that "The content of your post is awesome" Great work!data science course

    ReplyDelete
  34. Thanks for sharing great information. I like your blog and highly recommendData Science Training in Hyderabad

    ReplyDelete
  35. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.
    visit for more details

    ReplyDelete
  36. Very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing, data science course in Hyderabad

    ReplyDelete
  37. You really make it look so natural with your exhibition however I see this issue as really something which I figure I could never understand. It appears to be excessively entangled and incredibly expansive for me.360DigiTMG data scientist malaysia

    ReplyDelete
  38. This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data scientist course in hyderabad with placement

    ReplyDelete
  39. Amazing 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
    data science interview questions
    KNN Algorithm
    Logistic Regression explained

    ReplyDelete
  40. Extraordinary post. I simply found your blog and expected to express that I have genuinely refreshing scrutinizing your blog sections.Huge thankful for the significant data.

    HRDF training

    ReplyDelete
  41. Great post! I am actually getting ready to across this information, is very helpful my friend. Also great blog here with all of the valuable information you have. Keep up the good work you are doing here.data science courses

    ReplyDelete
  42. I was looking at a portion of your posts on this site and I consider this site is really enlightening! Keep setting up..
    https://360digitmg.com/course/data-analytics-using-python-r

    ReplyDelete
  43. Very nice blogs!!! i have to learning for lot of information for this sites…Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing, data sciecne course in hyderabad

    ReplyDelete
  44. Good Post! , it was so good to read and useful to improve my knowledge as an updated one, keep blogging.After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts likethis. https://www.3ritechnologies.com/course/data-science-online-training/

    ReplyDelete
  45. I found Hubwit as a transparent s ite, a social hub which is a conglomerate of Buyers and Sellers who are ready to offer online digital consultancy at decent cost.
    Best Institute for Data Science in Hyderabad

    ReplyDelete
  46. I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more.
    data scientist courses

    ReplyDelete
  47. very well explained. 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.
    Logistic Regression explained
    Correlation vs Covariance
    Simple Linear Regression
    data science interview questions
    KNN Algorithm
    Bag of Words Python

    ReplyDelete
  48. This is my first time visit here. From the tremendous measures of comments on your articles.I deduce I am not only one having all the fulfillment legitimately here!
    internet of things malaysia

    ReplyDelete
  49. Thanks for sharing great information. I highly recommend you.data science courses

    ReplyDelete
  50. Thanks for sharing the valuable information. it’s really helpful.Best data science courses in hyerabad

    ReplyDelete