Giter Site home page Giter Site logo

aws-messaging's Introduction

AWS Messaging

NPM

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
import { Consumer, Publisher } from 'aws-messaging';

// JavaScript
var Consumer = require('aws-messaging').Consumer;
var Publisher = require('aws-messaging').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 = new Publisher(/* awsRegion (default: us-east-1) */);
publisher.register('arn:aws:sns:us-east-1:000000000000:sns-topic', 'MyClass');

// Publishing with a registered class
var myClass = new MyClass();
publisher.send(myClass);

// Publishing directly to a topic
publisher.sendToTopic(myClass, topic);

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 = new Consumer('https://sqs.us-east-1.amazonaws.com/000000000000/sqs-topic', /* awsRegion (default: us-east-1) */, item => {
  // Perform work with the item
  return Promise.resolve();
});

// Start the consumer function
consumer.start();

// Optionally stop SQS consumption
consumer.stop();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.