sqs-producer-lib
Version:
Reusable AWS SQS producer library for Node.js microservices
134 lines (95 loc) โข 3.81 kB
Markdown
# ๐จ AWS SQS Client (Class-based)
A reusable Node.js library using a **class-based wrapper** to send messages to AWS SQS queues.
Seamlessly supports both **LocalStack for local testing** and **AWS SQS for production** with dynamic queue URLs.
## ๐ Features
- โ
Class-based, object-oriented design
- ๐ Works with both **LocalStack** and **AWS**
- โ๏ธ Customizable with constructor config
- ๐ก Helpful console logs and robust error handling
- ๐ฆ Easy to plug into any Node.js project
## ๐ฆ Installation
```bash
# Local path install
npm install /path/to/your/sqs-client-lib
# OR if published as npm package
npm install @your-org/sqs-client
```
## ๐ ๏ธ Usage
### 1. Import and create an instance of `SQSClient`
```js
const SQSClient = require('./src/SQSClient');
const sqsClient = new SQSClient({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_REGION || 'us-east-1',
useLocalstack: process.env.USE_LOCALSTACK === 'true',
localstackEndpoint: process.env.LOCALSTACK_ENDPOINT || 'http://localhost:4566',
accountId: process.env.AWS_ACCOUNT_ID || '000000000000',
});
```
### 2. Send a message to the queue
```js
const message = {
orderId: '123456',
action: 'order_placed',
};
sqsClient.sendMessage('your-queue-name', message)
.then((res) => console.log('โ
Message ID:', res.MessageId))
.catch((err) => console.error('โ Error:', err.message));
```
## โ๏ธ Config Options
You can pass these to the constructor:
| Option | Type | Description | Default |
|----------------------|----------|------------------------------------------|-------------------------|
| `accessKeyId` | string | AWS access key | **Required** |
| `secretAccessKey` | string | AWS secret key | **Required** |
| `region` | string | AWS region | `us-east-1` |
| `useLocalstack` | boolean | Enable LocalStack | `false` |
| `localstackEndpoint` | string | LocalStack endpoint | `http://localhost:4566` |
| `accountId` | string | AWS account ID | `000000000000` |
## ๐ Project Structure Example
```
your-project/
โโโ src/
โ โโโ SQSClient.js # This file
โโโ example.js # How you use the client
โโโ .env # Store environment variables here
โโโ README.md # You're here!
```
## ๐งช LocalStack Testing
### Step-by-step:
1. Run LocalStack via Docker
```bash
docker run --rm -it -p 4566:4566 localstack/localstack
```
2. Create a queue via AWS CLI:
```bash
aws --endpoint-url=http://localhost:4566 sqs create-queue --queue-name your-queue-name
```
3. Set `.env` values:
```env
USE_LOCALSTACK=true
AWS_ACCESS_KEY_ID=test
AWS_SECRET_ACCESS_KEY=test
AWS_REGION=us-east-1
LOCALSTACK_ENDPOINT=http://localhost:4566
AWS_ACCOUNT_ID=000000000000
```
## ๐ License
MIT License
ยฉ 2025 [Kenit Goswami](https://github.com/your-github)
## ๐โโ๏ธ Questions or Contributions?
Feel free to open issues or PRs. Feedback and improvements are always welcome!
## ๐ Useful Links
- [AWS SQS Documentation](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html)
- [LocalStack GitHub](https://github.com/localstack/localstack)
- [AWS SDK for JavaScript](https://github.com/aws/aws-sdk-js)