AWS Messaging
AWS messaging wrapper for multicasting published messages to consumer groups
Description
For low throughput messaging queues (e.g. domain object persistence topics, export notifications) Kinesis or Kafka aren't economical. Publishing messages to SNS and multicasting to different SQS queues (one for each consumer cluster) yields the same result of published messages being sent to multiple application clusters.
This library abstracts away the SNS sending and SQS polling instead providing a Publisher class and a Consumer class. A good article describing this technique is http://docs.aws.amazon.com/sns/latest/dg/SendMessageToSQS.html.
Usage
NOTE: This library uses default AWS credentials through the environment variables AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
.
// TypeScript; // JavaScriptvar Consumer = Consumer;var Publisher = Publisher;
Publishing to SNS Topics
First create an AWS SNS topic and pull it's SNS topic ARN. Create a Publisher
and register a JavaScript object class with that topic (or skip this step and send objects directly to that topic as shown further down):
var publisher = /* awsRegion (default: us-east-1) */;publisher; // Publishing with a registered classvar myClass = ;publisher; // Publishing directly to a topicpublisher;
Consuming from SQS Topics
First create an AWS SQS topic, subscribe it to your SNS topic, and give the SNS topic publish permissions to the SQS topic (details can be found in http://docs.aws.amazon.com/sns/latest/dg/SendMessageToSQS.html). NOTE: Make sure to set the SQS Receive Message Wait Time to 20s for long polling and to reduce the total number of SQS API calls (which you are billed for).
var consumer = 'https://sqs.us-east-1.amazonaws.com/000000000000/sqs-topic' /* awsRegion (default: us-east-1) */ { // Perform work with the item return Promise;}; // Start the consumer functionconsumerstart; // Optionally stop SQS consumptionconsumer;