@janiscommerce/sqs-emitter v1.1.0
sqs-emitter
Installation
npm install @janiscommerce/sqs-emitterUsage
const SqsEmitter = require('@janiscommerce/sqs-emitter');SQS Emitter
This class is compatible with @janiscommerce/api-session. If it's instanciated using
getSessionInstance, a message attributejanis-clientwith the session'sclientCodewill be automatically added to every event.The event
contentwill be JSON-stringified before sendingThe event
attributescan 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
payloadFixedPropertiesproperty 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'
 * 		}
 * 	]
 * }
 */