2.4.3 • Published 11 months ago

@luxuryescapes/lib-events v2.4.3

Weekly downloads
26
License
UNLICENSED
Repository
-
Last release
11 months ago

lib-events

lib-events is a helper to dispatch events using AWS SNS and to listen to these events using AWS SQS.

For a complete guide on how to set it up, please refer to: Publishing and Consuming events.

Creating the Publisher

The way we "dispatch" an event is by publishing it to Amazon SNS. First of all, you need to create a publisher, it will be your connection to SNS:

import { createPublisher } from 'lib-events';

const publisher = createPublisher({
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  region: 'ap-southeast-2',
  topic: 'arn:aws:sns:ap-southeast-2:1234:my-sns-topic',
  apiHost: 'https://our-api.com'
})

The createPublisher method allows you to pass one more parameter called sessionToken, you have to inform it if you are running it in dev or if you are assuming an IAM role.

To get temporary accessKeyId, secretAccessKey and sessionToken for your account (recommended), follow this guide: Authenticate access using MFA through the AWS CLI.

Below is an example using an IAM role:

const role = getRoleCredentials();

const publisher = createPublisher({
  accessKeyId: role.Credentials.AccessKeyId,
  secretAccessKey: role.Credentials.SecretAccessKey,
  sessionToken: role.Credentials.SessionToken,
  region: 'ap-southeast-2',
  topic: 'arn:aws:sns:ap-southeast-2:1234:my-sns-topic',
  apiHost: 'https://our-api.com'
})

Dispatching Events

Now that you created the publisher, you can use its instance to dispatch events:

import { createPublisher, ORDER_CREATED } from 'lib-events';

const publisher = createPublisher({ ... })

publisher.dispatch({
  type: ORDER_CREATED,
  uri: `/api/orders/${order.id_orders}`,
  checksum: order.checksum,
  source: process.env.HEROKU_APP_NAME,
  message: `${user.fullname} just purchased ${order.offer.name}`
})

Publishing to a fifo queue

const publisher = createPublisher({
  ...,
  topic: 'arn:aws:sns:ap-southeast-2:1234:my-fifo-topic.fifo'
})

publisher.dispatch({
  type: ORDER_CREATED,
  uri: `/api/orders/${order.id_orders}`,
  checksum: order.checksum,
  source: process.env.HEROKU_APP_NAME,
  message: `${user.fullname} just purchased ${order.offer.name}`,
  transactionId: '123456', // this is used for deduplication, required for fifo queues
  groupId: '123'// this is used for partitioning, required for fifo queues
})

Creating the Consumer

We "listen" to events by consuming an Amazon SQS queue. To do that, create a consumer:

import { createConsumer } from 'lib-events';

const consumer = createConsumer({
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  region: 'ap-southeast-2',
  queueUrl: 'https://sqs.ap-southeast-2.amazonaws.com/1234/my-sqs-name'
})

As in Creating the Publisher, you might need to inform sessionToken depending on your account type.

Listening to Events

With the consumer instance, you can now listen to the SQS events. The common way to do that, is by running a worker process to constantly poll for messages in the consumer and process them accordingly:

import { createConsumer, ORDER_CREATED } from 'lib-events';

const consumer = createConsumer({ ... })

async function processMessages({ type, source, id, checksum }, ack) {
  switch (type) {
    case (ORDER_CREATED):
      console.log(`${source} created an order!`);
      break;
    default:
      break;
  }

  await ack()
}

exports.process = async function () {
  await consumer.poll(processMessages, {
    maxNumberOfMessages: 10,
    maxIterations: 10
  });
}

Running tests

yarn test

Publishing

Update the version in package.json as part of your PR and CircleCI will do the rest.

2.4.1

11 months ago

2.4.0

12 months ago

2.4.3

11 months ago

2.4.2

11 months ago

2.3.0

1 year ago

2.2.6

1 year ago

2.2.5

1 year ago

2.2.4

1 year ago

2.2.3

1 year ago

2.2.2

1 year ago

2.2.1

2 years ago

2.2.0

2 years ago

2.1.8

2 years ago

2.1.9

2 years ago

2.1.6

2 years ago

2.1.7

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.5

2 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.0.40

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.33

4 years ago

1.0.27

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.16

4 years ago

1.0.12

4 years ago