1.1.0 • Published 5 months ago

@janiscommerce/sqs-emitter v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

sqs-emitter

Build Status Coverage Status npm version

Installation

npm install @janiscommerce/sqs-emitter

Usage

const SqsEmitter = require('@janiscommerce/sqs-emitter');

SQS Emitter

This class is compatible with @janiscommerce/api-session. If it's instanciated using getSessionInstance, a message attribute janis-client with the session's clientCode will be automatically added to every event.

The event content will be JSON-stringified before sending

The event attributes can be either Strings or Arrays. It's important to note that using other data types may cause issues or inconsistencies in the implemented filter policies. Ensure that the values provided for the attributes are always of the expected type to avoid errors in message processing.

The payloadFixedProperties property must be an array of strings specifying the content properties that must be mandatorily sent. This improves error management by enabling us to identify which data failed and decide accordingly.

Publish single event

const { SqsEmitter } = require('@janiscommerce/sqs-emitter');

const sqsEmitter = this.session.getSessionInstance(SqsEmitter);

const result = await sqsEmitter.publishEvent('https://sqs.us-east-1.amazonaws.com/123456789012/MySQSName', {
  content: {
    id: '1'
  },
  attributes: {
    source: 'user',
    platforms: ['mobile', 'web']
  },
  payloadFixedProperties: ['id']
});

/**
 * Sample Output
 *
 * {
 * 	MessageId: '8563a94f-59f3-4843-8b16-a012867fe97e',
 * 	SequenceNumber: '' // For FIFO topics only
 * }
 */

Publish multiple events

This method will send multiple events in one SDK call. It will also separate in batches when the total size limit of 256KB payload size is exceeded. Batches will be sent with a smart concurrency protocol (optimizing calls with a maximum of 25 concurrent calls).

const { SqsEmitter } = require('@janiscommerce/sqs-emitter');

const sqsEmitter = this.session.getSessionInstance(SqsEmitter);

const result = await sqsEmitter.publishEvents('https://sqs.us-east-1.amazonaws.com/123456789012/MySQSName', [
  {
    content: {
      id: '1'
    },
    attributes: {
      source: 'user',
      platform: 'mobile'
    },
    payloadFixedProperties: ['id']
  },
  {
    content: {
      id: '2'
    },
    attributes: {
      source: 'user',
      platform: 'mobile'
    },
    payloadFixedProperties: ['id']
  }
]);

/**
 * Sample Output
 *
 * {
 *   successCount: 1,
 *   failedCount: 1,
 *   success: [
 * 		{
 * 			Id: '1',
 * 			messageId: '8563a94f-59f3-4843-8b16-a012867fe97e'
 * 		}
 * 	],
 * 	failed: [
 * 		{
 * 			Id: '2',
 * 			errorCode: 'SQS001',
 * 			errorMessage: 'SQS Failed'
 * 		}
 * 	]
 * }
 */