sqs-producer
Enqueues messages onto a given SQS queue
Installation
npm install sqs-producer
Note
This library assumes you are using AWS SDK v3. If you are using v2, please install v2.2.0:
npm install sqs-producer@2.2.0 --save-dev
Usage
import { Producer } from 'sqs-producer';
import { SQSClient } from '@aws-sdk/client-sqs';
const producer = Producer.create({
queueUrl: 'https://sqs.eu-west-1.amazonaws.com/account-id/queue-name',
region: 'eu-west-1'
});
await producer.send(['msg1', 'msg2']);
const size = await producer.queueSize();
console.log(`There are ${size} messages on the queue.`);
await producer.send([
{
id: 'id1',
body: 'Hello world'
}
]);
await producer.send([
{
id: 'id1',
body: 'Hello world with two string attributes: attr1 and attr2',
messageAttributes: {
attr1: { DataType: 'String', StringValue: 'stringValue' },
attr2: { DataType: 'Binary', BinaryValue: new Buffer('binaryValue') }
}
},
{
id: 'id2',
body: 'Hello world delayed by 5 seconds',
delaySeconds: 5
}
]);
await producer.send({
id: 'testId',
body: 'Hello world from our FIFO queue!',
groupId: 'group1234',
deduplicationId: 'abcdef123456'
});
Credentials
By default the consumer will look for AWS credentials in the places specified by the AWS SDK. The simplest option is to export your credentials as environment variables:
export AWS_SECRET_ACCESS_KEY=...
export AWS_ACCESS_KEY_ID=...
If you need to specify your credentials manually, you can use a pre-configured instance of the SQS Client client.
import { Producer } from 'sqs-producer';
import { SQSClient } from '@aws-sdk/client-sqs';
const producer = Producer.create({
queueUrl: 'https://sqs.eu-west-1.amazonaws.com/account-id/queue-name',
region: 'eu-west-1',
sqs: new SQSClient({
region: 'my-region',
credentials: {
accessKeyId: 'yourAccessKey',
secretAccessKey: 'yourSecret'
}
})
});
await producer.send(['msg1', 'msg2']);
Development
Test
npm test
Coverage
For coverage report, run the command:
npm run coverage
Lint
To check for problems using ESLint
npm run lint
Contributing
See contributing guildlines