What is sqs-producer?
The sqs-producer npm package is a simple library for sending messages to an AWS SQS (Simple Queue Service) queue. It provides a straightforward API for creating and sending messages, making it easier to integrate SQS into your Node.js applications.
What are sqs-producer's main functionalities?
Create a Producer
This feature allows you to create a producer instance that is configured to send messages to a specific SQS queue. You need to provide the queue URL and the AWS region.
const { Producer } = require('sqs-producer');
const producer = Producer.create({
queueUrl: 'https://sqs.us-east-1.amazonaws.com/account-id/queue-name',
region: 'us-east-1'
});
Send a Single Message
This feature allows you to send a single message to the SQS queue. You need to provide a message ID and the message body.
producer.send({
id: 'id1',
body: 'Hello SQS!'
}, (err) => {
if (err) console.error(err);
else console.log('Message sent successfully');
});
Send Multiple Messages
This feature allows you to send multiple messages in a single batch to the SQS queue. You need to provide an array of messages, each with a unique ID and message body.
producer.send([{
id: 'id1',
body: 'Hello SQS!'
}, {
id: 'id2',
body: 'Hello again!'
}], (err) => {
if (err) console.error(err);
else console.log('Messages sent successfully');
});
Send Messages with Delay
This feature allows you to send a message to the SQS queue with a delay. You need to provide the message ID, message body, and the delay in seconds.
producer.send({
id: 'id1',
body: 'Hello SQS!',
delaySeconds: 10
}, (err) => {
if (err) console.error(err);
else console.log('Message sent successfully with delay');
});
Other packages similar to sqs-producer
aws-sdk
The aws-sdk package is the official AWS SDK for JavaScript. It provides comprehensive support for all AWS services, including SQS. While it is more feature-rich and supports a wider range of AWS services, it is also more complex to use compared to sqs-producer.
sqs-consumer
The sqs-consumer package is a library for consuming messages from an AWS SQS queue. It is designed to work in conjunction with sqs-producer, providing a simple API for receiving and processing messages. While sqs-producer focuses on sending messages, sqs-consumer focuses on receiving them.
node-sqs
The node-sqs package is another library for interacting with AWS SQS. It provides a simple API for sending and receiving messages, similar to sqs-producer. However, it is less popular and less actively maintained compared to sqs-producer.
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@5.8.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