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
Usage
const { Producer } = require('sqs-producer');
const producer = Producer.create({
queueUrl: 'https://sqs.eu-west-1.amazonaws.com/account-id/queue-name',
region: 'eu-west-1'
});
const producer = Producer.create({
queueUrl: 'https://sqs.eu-west-1.amazonaws.com/account-id/queue-name',
region: 'eu-west-1',
accessKeyId: 'yourAccessKey',
secretAccessKey: 'yourSecret'
});
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({
body: 'Hello world from our FIFO queue!',
groupId: 'group1234',
deduplicationId: 'abcdef123456'
});
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